iOS使后台可以运行代码 比如定时器

2021-10-29 10:59:45 浏览数 (1)

代码语言:javascript复制
//后台运行定时器
-(void)applicationDidEnterBackground:(UIApplication *)application{

UIApplication*   app = [UIApplication sharedApplication];

__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;

        }

    });

});

 
</pre> 

0 人点赞