BackgroundFetch

956 阅读1分钟

Background Fetch

- (**BOOL**)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[application setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];
return YES;
}
-(void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{
//刷新数据
//UIBackgroundFetchResultNewData,//刷新到新数据
//UIBackgroundFetchResultNoData,//没有新数据
//UIBackgroundFetchResultFailed//刷新失败
completionHandler(UIBackgroundFetchResultNewData);
}

在XCode->TARGETS->Capabilities->Background Modes,选择Background Fetch打勾。

Fetch事件是由系统管理的,开发者无法预先知道Fetch事件达到的时机。但XCode也提供了Fetch事件的调试办法,在XCode上运行程序后,在Debug->Simulate Background Fetch。还有一种情况是app没有运行在后台由于内存吃紧被系统kill的情况下(非用户通过App Switcher主动kill)被Fetch事件唤醒执行。这种情况的测试方法如下:Product->Scheme->Edit scheme 在Debug模式选中Options,点选Launch due to a background fetch event,运行即可。