ksnowlv

回顾过去,总结以往;立足现在,铭记当下;技术为主,笔记而已.

iOS有限后台任务

| Comments

iOS下有限后台任务如何使用呢?

iOS应用,在切换到后台时,可以开启有限后台任务。 见苹果开发文档

一.几个相关方法

  • 1.beginBackgroundTaskWithExpirationHandler

  • 2.beginBackgroundTaskWithName:expirationHandler

  • 3.endBackgroundTask
  • 4.setMinimumBackgroundFetchInterval

二.切换到后台的处理

1
2
3
4
5
6
7
8
9
10
11
- (void)applicationDidEnterBackground:(UIApplication *)application {

    self.backgroundTaskIdentifier =
    [application beginBackgroundTaskWithExpirationHandler:^{
        [application endBackgroundTask:self.backgroundTaskIdentifier];
        self.backgroundTaskIdentifier = UIBackgroundTaskInvalid;
    }];

  //添加自己的处理逻辑

}

三.切换到前台的处理

1
2
3
4
5
6
7
8
- (void)applicationWillEnterForeground:(UIApplication *)application {

   if (self.backgroundTaskIdentifier != UIBackgroundTaskInvalid){
         [application endBackgroundTask:self.backgroundTaskIdentifier];
         self.backgroundTaskIdentifier = UIBackgroundTaskInvalid;
   }

}

Comments

comments powered by Disqus
Included file 'custom/after_footer.html' not found in _includes directory