首页 > 代码库 > iOS- 使后台可以运行代码 比如定时器
iOS- 使后台可以运行代码 比如定时器
//后台运行定时器
-(void)applicationDidEnterBackground:(UIApplication *)application{
UIApplication* app = [UIApplicationsharedApplication];
__block UIBackgroundTaskIdentifier bgTask;
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
dispatch_async(dispatch_get_main_queue(), ^{
if (bgTask != UIBackgroundTaskInvalid)
{
bgTask = UIBackgroundTaskInvalid;
}
});
}];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{
dispatch_async(dispatch_get_main_queue(), ^{
if (bgTask != UIBackgroundTaskInvalid)
{
bgTask = UIBackgroundTaskInvalid;
}
});
});
}
iOS- 使后台可以运行代码 比如定时器