How send application to background when install on device?

时光怂恿深爱的人放手 提交于 2019-12-24 00:39:16

问题


I am making an application in which i want features like as when i run my app on device then it will closed immediately without showing any screen.But Application works in background. When user click on icon of application then it will not show any screen but work in background. After 2 minutes gap it will show a alert message. How do that?

I have used code for this given below:-

-(void)applicationDidFinishLaunching:(UIApplication *)application{
[application cancelAllLocalNotifications];
[self applicationWillTerminate:application];}-(void)applicationWillTerminate:(UIApplication *)application{
/*
 Called when the application is about to terminate.
 Save data if appropriate.
 See also applicationDidEnterBackground:.
 */

UILocalNotification* ln = [[UILocalNotification alloc] init];
ln.fireDate =[NSDate dateWithTimeIntervalSinceNow:30];
ln.alertBody = [NSString stringWithFormat:@"Now app is working in Background."];         
ln.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] scheduleLocalNotification:ln];
ln.hasAction=NO;
[ln release];
exit(0);}

But this is not working as i want. So what is bug in this code? How do that?

Thanks in advance...


回答1:


You can't put your app away by manually calling [self applicationWillTerminate:application];. It's a delegate method that gets called when your application is about to be terminated, not a method to terminate the app.

You could try to schedule a local notification in didFinishLaunchingWithOptions: and call exit(0); afterwards. Some kind of splah screen (or black screen) will probably be shown for a moment.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

  [application cancelAllLocalNotifications];
  UILocalNotification* ln = [[UILocalNotification alloc] init];
  ln.fireDate =[NSDate dateWithTimeIntervalSinceNow:30];
  ln.alertBody = [NSString stringWithFormat:@"Now app is working in Background."];         
  ln.soundName = UILocalNotificationDefaultSoundName;
  [[UIApplication sharedApplication] scheduleLocalNotification:ln];
  ln.hasAction=NO;
  [ln release];
  exit(0);    //this line kills the app (and gets your app rejected)
  return NO;  //this line is just to make compiler happy
}

Please note that this will most definetly not be approved for App Store.



来源:https://stackoverflow.com/questions/9664595/how-send-application-to-background-when-install-on-device

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!