如何在才能在后台applicationDidEnterBackground长久运行
我查阅了苹果官网关于ios后台运行的方法,苹果仅允许(local,music,voip,notifaction)机制在后台运行。苹果商店上有应用程序实现了后台长久运行,不知道是如何实现,请问那位大侠知道???
现在我仅能做到的后台运行十分钟的代码如下:
UIBackgroundTaskIdentifier backgroundTask;
- (void)applicationDidEnterBackground:(UIApplication *)application
{
backgroundTask = [application beginBackgroundTaskWithExpirationHandler: ^{
// 如果超时这个block将被调用
dispatch_async(dispatch_get_main_queue(), ^{
if (backgroundTask != UIBackgroundTaskInvalid)
{
// do whatever needs to be done
[application endBackgroundTask:backgroundTask];
backgroundTask = UIBackgroundTaskInvalid;
}
});
}];
// Start the long-running task
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Do the work!
[NSThread sleepForTimeInterval:5];
NSLog(@"Time remaining: %f",[application backgroundTimeRemaining]);
[NSThread sleepForTimeInterval:5];
NSLog(@"Time remaining: %f",[application backgroundTimeRemaining]);
[NSThread sleepForTimeInterval:5];
NSLog(@"Time remaining: %f",[application backgroundTimeRemaining]);
while(1)
{
[NSThread sleepForTimeInterval:5];
NSLog(@"Time remaining: %f",[application backgroundTimeRemaining]);
}
// done!
// call endBackgroundTask - should be executed back on
// main thread
dispatch_async(dispatch_get_main_queue(), ^{
if (backgroundTask != UIBackgroundTaskInvalid)
{
// if you don't call endBackgroundTask, the OS will exit your app.
[application endBackgroundTask:backgroundTask];
backgroundTask = UIBackgroundTaskInvalid;
}
});
});
NSLog(@"Reached the end of ApplicationDidEnterBackground - I'm done!");
} --------------------编程问答-------------------- 因为后台运行的只有播放音乐的进程没有限制,楼主可以当快要退出时,播放一段没有声音的音乐,这样就不会被退出 --------------------编程问答-------------------- 后台播放音乐,是可以长久运行,
但是问题是当你打开别的有关播放音乐的其它应用程序的话,再关掉这个应用程序,后台也就跟着关闭了,所以音乐后台是不行了 --------------------编程问答-------------------- 高手在那?自己顶个。。。。。
补充:移动开发 , iPhone