iOS后台任务

来源:互联网 发布:文明5 mac版 编辑:程序博客网 时间:2024/04/29 16:16

iOS后台任务

ios后台运行分方式有三种

  • 1 永远运行
  • 2 短暂运行
  • 3 立即挂起

后台永远运行分为这几种情况

  • 播放音频文件(playing audio
  • 获取定位更新(getting location updates)

  • 杂志app中下载新的期刊(downloading new issues for newsstand apps)

  • VoIP 呼叫(handing VoIP calls)

实现方式

  • 1 适配Info.plist 文件

    • info.plist中的 Required background modes key 类型为 NSArray
    • 在其中添加Value 为App registers for location updates 设定后台定位
    • 在其中添加Valur 为 App plays audio or streams audio/video using AirPlay 设定后台音乐播放
  • 2 如果是定位,申请定位权限。 播放音乐申请后台播放权限。具体看具体代码


后台短暂运行

App 进入后台默认情况会立即挂起,不会再运行情况。 后台短暂运行App 进入后台,系统会让其在运行一段时间在挂起。

实现方式

// 接收进入后台通知        [[NSNotificationCenter defaultCenter] addObserver:self                                                 selector:@selector(backgroundCleanDisk)                                                     name:UIApplicationDidEnterBackgroundNotification                                                   object:nil];// 后台处理- (void)backgroundCleanDisk {      UIApplication*  app = [UIApplication sharedApplication];    __block UIBackgroundTaskIdentifier bgTask;     bgTask = [app beginBackgroundTaskWithExpirationHandler:^{        [app endBackgroundTask:bgTask];s        bgTask = UIBackgroundTaskInvalid;    }];   }

1 0
原创粉丝点击