我正在创建一个应用程序,其中我需要的功能就像当我在设备上运行我的应用程序然后它将立即关闭而不显示任何屏幕。但应用程序在后台工作。当用户点击应用程序图标时,它不会显示任何屏幕,但在后台工作。 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);}
但这不符合我的要求。那么这段代码中的bug是什么?怎么样?
提前致谢...
答案 0 :(得分: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。