IOS backgroundfetch

从ios7 开始,苹果为了优化后台逻辑,新增了backgroundfetch这种模式。我们就来看看是怎么一回事吧!

要想让自己的app支持 backgroundfetch,需要在xcode 当中配置一下。在target->Capabilities->backgroundModes打开。勾选上backgroundfetch选项。

接下来,就是写代码了~~~ 为了保证后台获取尽可能多次的进行。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

加上这句话

[[UIApplication sharedApplication] setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];

然后我们需要实现一个委托函数,就是你需要在后台获取的时候做什么事情。

-(void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
    if ([UIApplication sharedApplication].backgroundRefreshStatus == UIBackgroundRefreshStatusAvailable)
    {
        LoadingViewController* vc = ((UINavigationController*)self.window.rootViewController).topViewController;
        [vc updateImage];
        completionHandler(UIBackgroundFetchResultNewData);
    }
}
这里的updateImage 函数会去获取一个网络图片资源。然后更新界面。

最后别忘了调用

completionHandler(UIBackgroundFetchResultNewData);

否则界面不会在后台刷新。运行app 之后 home切换到后台,然后会不定时的获取内容。

如果你没有耐心的话,可以通过xcode 手动触发此场景 Debug->simulator BackgroundFetch


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值