iOS后台运行示例代码

iOS获取有限的后台运行时间

// AppDelegate.h文件 
@property (assign, nonatomic) UIBackgroundTaskIdentifier backgroundUpdateTask; 
 
// AppDelegate.m文件 
- (void)applicationDidEnterBackground:(UIApplication *)application 
{ 
    [self beingBackgroundUpdateTask]; 
    // 后台运行代码 
    [self endBackgroundUpdateTask]; 
} 
 
- (void)beingBackgroundUpdateTask 
{ 
    self.backgroundUpdateTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{ 
        [self endBackgroundUpdateTask]; 
    }]; 
} 
 
- (void)endBackgroundUpdateTask 
{ 
    [[UIApplication sharedApplication] endBackgroundTask: self.backgroundUpdateTask]; 
    self.backgroundUpdateTask = UIBackgroundTaskInvalid; 
} 

猜你喜欢

转载自wangleyiang.iteye.com/blog/2106999