2019-01-22ISO实现段时间内后台运行方式

2019-01-22  本文已影响5人  不暗_黑夜

ISO实现段时间内后台运行方式

- (void)applicationDidEnterBackground:(UIApplication *)application {

    UIApplication*  app = [UIApplication sharedApplication];

    __block    UIBackgroundTaskIdentifier backgroundTask;

    backgroundTask = [app beginBackgroundTaskWithExpirationHandler:^{

        dispatch_async(dispatch_get_main_queue(), ^{

if(backgroundTask != UIBackgroundTaskInvalid)

            {

                backgroundTask = UIBackgroundTaskInvalid;

            }

        });

}];

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{

        dispatch_async(dispatch_get_main_queue(), ^{

if(backgroundTask != UIBackgroundTaskInvalid)

            {

                backgroundTask = UIBackgroundTaskInvalid;

        });

    });

}

用这种方式可以实现app在段时间内不会被iOS挂起, 但一般时间不会超过10分钟

上一篇 下一篇

猜你喜欢

热点阅读