【问题标题】:How send application to background when install on device?在设备上安装时如何将应用程序发送到后台?
【发布时间】:2012-03-12 09:35:50
【问题描述】:

我正在制作一个应用程序,其中我想要一些功能,例如当我在设备上运行我的应用程序时,它将立即关闭而不显示任何屏幕。但应用程序在后台运行。当用户点击应用程序的图标时,它不会显示任何屏幕,而是在后台工作。间隔 2 分钟后,它将显示一条警报消息。怎么做?

我已经使用了下面给出的代码:-

-(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);}

但这不是我想要的。那么这段代码中的错误是什么?怎么做?

提前谢谢...

【问题讨论】:

    标签: iphone uiapplicationdelegate uiapplication


    【解决方案1】:

    您无法通过手动调用 [self applicationWillTerminate:application]; 来收起您的应用。它是在您的应用程序即将终止时调用的委托方法,而不是终止应用程序的方法。

    您可以尝试在didFinishLaunchingWithOptions: 中安排本地通知,然后致电exit(0);。可能会暂时显示某种 splah 屏幕(或黑屏)。

    - (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
    }
    

    请注意,这绝对不会被 App Store 批准。

    【讨论】:

    • 我可以证明,它肯定会被苹果拒绝。当通话结束时,我想用假通话应用程序做类似的事情。他们检查所有 exit(0) 调用,如果发现任何调用,他们会立即拒绝该应用程序。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-18
    • 2018-05-20
    • 1970-01-01
    相关资源
    最近更新 更多