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

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

//后台运行定时器

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

            }

        });

    });