在分布式处理控制执行程序( didFinishLaunchingWithOptions )中,每隔1分钟调用一次函数httpRequest的定时器循环。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//rest of code
NSTimer *notifyTimer = [NSTimer timerWithTimeInterval:60 target:self selector:@selector(httpRequest) userInfo:nil repeats:YES];//7200.0
[[NSRunLoop mainRunLoop] addTimer:notifyTimer forMode:NSDefaultRunLoopMode];
return YES;
}按下主页按钮后,应用程序将转到后台并调用函数applicationDidEnterBackground,因此后台任务正在启动。
- (void)applicationDidEnterBackground:(UIApplication *)application
{
__block UIBackgroundTaskIdentifier bgTask;
UIApplication *app = [UIApplication sharedApplication];
expirationHandler = ^{
[app endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
bgTask = [app beginBackgroundTaskWithExpirationHandler:expirationHandler];
};
bgTask = UIBackgroundTaskInvalid;
bgTask = [app beginBackgroundTaskWithExpirationHandler:expirationHandler];
}通过httpRequest函数,我每隔1分钟从web服务器获取一次Y,因此每隔几秒钟就会触发一次UILocalNotification。
-(NSString *)httpRequest {
NSURL *url = [NSURL URLWithString:@"http://192.168.10.67/t.php"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
NSString *userAgent = [NSString stringWithFormat:@"bgTaskTest-IOS"];
[request setValue:userAgent forHTTPHeaderField:@"User-Agent"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
[request setHTTPMethod:@"GET"];
[request setTimeoutInterval:25];
NSURLResponse *response;
NSData *dataReply = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
NSString *stringReply = [[NSString alloc] initWithData:dataReply encoding:NSASCIIStringEncoding];
if ([stringReply isEqualToString:@"Y"]) {
[self showLocalNotification:nil]; //calling UILocalNotification
} else {
NSLog(@"%@",stringReply);
}
return stringReply;
}根据httpRequest函数的响应,每隔1分钟调用一次函数showLocalNotification。
-(void)showLocalNotification {
NSString *msg = @"test message";
[[UIApplication sharedApplication] cancelAllLocalNotifications];
UILocalNotification *_localNotification = [[UILocalNotification alloc]init];
_localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:1];
_localNotification.timeZone = [NSTimeZone defaultTimeZone];
_localNotification.alertBody = msg;
_localNotification.soundName = UILocalNotificationDefaultSoundName;
_localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber]+1;
[[UIApplication sharedApplication] scheduleLocalNotification:_localNotification];
//[[UIApplication sharedApplication] presentLocalNotificationNow:_localNotification];
}一切正常,当应用程序在后台时,每隔1分钟就会有通知提示。
但我的问题是后台任务的生命周期是10分钟,所以10分钟后没有通知提示。由于这个原因,我在beginBackgroundTaskWithExpirationHandler中再次启动后台任务,但我的应用程序在重新启动后台任务时终止。
当应用程序在后台时,我不能使用超过10分钟的通知。
请任何人帮帮我。
发布于 2013-11-04 16:33:48
没有办法(在应用商店的指导方针内)在后台运行任意代码超过10分钟(如您所注意到的)。
10分钟后,您的应用程序将被挂起。有几种方法可以绕过这一点,注册其他后台模式(如背景音频,连续播放静音文件)或后台voip或后台定位服务。
这些繁琐的工作将使您的应用程序保持未挂起的,但是您的应用程序将不会获得商店的批准。
在iOS7中,在后台运行代码有一些改进,但是没有什么可以做你想做的事情。
因此,如果这是一个你自己使用的应用程序,使用私有API或我上面建议的方法,但是如果你想把这个应用程序放在商店里,我担心你运气不好。
https://stackoverflow.com/questions/19762071
复制相似问题