iOS 7 SDK: 如何使用后台获取(Background Fetch)
2015-06-03 15:17
633 查看
转载自:http://www.cocoachina.com/industry/20131114/7350.html
本文主要教你如何使用iOS 7 SDK多任务处理API--Background Fetch。我们生活在一个社交化的世界中,大部分用户都安装了几个社交类app,但是每次用户打开app,他们必须要等待app加载更新才能看到更新
本文主要教你如何使用iOS 7 SDK多任务处理API--Background Fetch。我们生活在一个社交化的世界中,大部分用户都安装了几个社交类app,但是每次用户打开app,他们必须要等待app加载更新才能看到跟更多最新的内容,对于越来越没耐心的用户来说这一点无疑令人非常痛苦。现在,iOS 7的后台获取(Background Fetch)可以很好地解决这个问题,在用户打开应用之前,app就能自动更新获取内容。
以检测流量的app为例来说明Background Fetch如何工作。如果你会在每天早上查看应用,我们假设在8:20 AM,,你的iOS app必须在当时获得信息。现在如果操作系统知道你将会在8:20 AM左右使用app,那么它可以提前获得数据,从而提供更好的用户体验。
关于iOS 7多任务执行更全面的概览可参看我们的主题“iOS
7 SDK: Multitasking Enhancements”。以下我们将会以一个实例工程来演示如何使用后台获取(Background Fetch)。
1.项目安装
第一步是创建一个iOS 7项目,并选择单视图app,接着添加一些有用的属性:
NSMutablearray对象将会被用来在TableView中保存对象列表。在这个教程中,你将不能调用任何服务来获得数据。相反,你将使用possibleTableData数组,并随机从中选择几个对象。整个numberOfnewPosts代表新发布的内容--每次进行请求或者接收后台获取时可用。refrestControl是一个在更新任务时使用的控件。由于不在教程之内,所以本文不会在此展开。
在Main.storyboard中,把ViewController改为UITableViewController,下一步,点击UITableViewController,转到Editor > Embed in > Navigation Controller。记得把自定义类设置为ViewController。然后转至ViewController.m,第一步加载一些数据。以下代码将会申请内存并创建数据对象,创建一个标题以及初始化refreshControl:
以上代码将会产生一个提醒,因为我们丢失了insertNewObject method。让我们来解决它。该方法将会产生一个随机数,并且将从日期数组获得对象相同的数据,然后它将会通过新值来更新tableview。
当你添加以下方法时,getRandomNumberBetween提醒将会被禁止:
为了在 NSArray object上加载对象,我们需要执行TableView委托函数。
非常简单吧?如果运行项目,你会看到一个类似下图的界面:
2. Background Fetch
现在开始创建Background Fetch功能,首先从Project开始,接着是Capabilities,然后Put Background Modes ON,再选择Background Fetch,如下图所示:
但仅仅做这个是不够的。默认地,app不会调用后台API,所以你需要在AppDelegate.m文件中把以下代码添加至-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions method.
这个可以让系统决定何时应该展示新内容。现在你的app已经知道启动ackground fetch,让我们告诉它要做些什么。方法-(void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler将会对你有所帮助。每当执行后台获取时该方法都会被调用,并且应该被包含在AppDelegate.m文件中。以下是完整版本:
下一步你应该也把ViewController头文件放进AppDelegate.m类。
注意insertNewObjectForFetchWithCompletionHandler并没有被创建,所以还需要在ViewController.h中声明它。
现在关注执行文件,类似于之前insertNewObject调用的添加。我们使用completionHandler来和系统“交流”,并让它告诉我们app是否现在获取数据,或者当前是否有有效数据。
完成代码,现在我们模拟一个测试,并验证所有项目都能启动和运行。
3. Simulated Background Fetch
如果想确定是否每件事都已经配置好了,你需要编辑Schemes,在Schemes列表点击Manage Schemes选项,如下:
在Schemes管理区你可以复制app的scheme:
复制后scheme会在新窗口展示。你可在Options标签下更改它的名称。选择“Launch due to a background fetch event”框,并在所有窗口中点击“OK”。
接着,使用复制的scheme运行app。注意app不会在前台打开,但是它应该已经获得了一些内容。如果打开app,并且几个recipe已生效,那就说明操作已经成功了。为了使用后台获取功能,你也可以从Xcode菜单的Debug > Simulate Background Fetch开始。
************************************************
转自:http://www.tekuba.net/program/320/
开启Background Fetch支持
在XCode->TARGETS->Capabilities->Background Modes打开并添加Background Fetch.
同时在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions方法中添加:
[[UIApplication sharedApplication] setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];
MinimumBackgroundFetchInterval参数值是两次Fetch时间间隔,不能保证每隔这个时间间隔都会调用。这里设置为UIApplicationBackgroundFetchIntervalMinimum,意思是尽可能频繁的调用我们的Fetch方法。
增加实现Fetch方法
- (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{};
每次系统Fetch时都会调用该方法,我们可以在该方法中做刷新数据等操作,操作执行完成以后要调用completionHandlerblock(),比如:completionHandler(UIBackgroundFetchResultNewData);文档中说系统会根据completionHandler(执行的时间)来估计此次Fetch的耗电等。如果耗时耗电比较多,可能会降低被调用的次数。但这个方法也不是不限时执行的,说是有30s的时间来执行操作。completionHandler有三个参数:
UIBackgroundFetchResultNewData 成功拉取数据
UIBackgroundFetchResultNoData 没有新数据
UIBackgroundFetchResultFailed 拉取数据失败或者超时
模拟Fetch事件
在实际的IOS7环境中,Fetch事件是由系统管理的,app开发者无法预先知道Fetch事件达到的时机。但XCode也提供了Fetch事件的调试办法,在XCode上运行程序后,在Debug->Simulate Background Fetch.
还有一种情况是app没有运行(不在前台也不在后台),被Fetch事件唤醒执行.这种情况的测试方法如下:
Product->Scheme->Edit scheme 在Debug模式选中Options,点选Launch due to a background fetch event,运行即可。
]可以观察到当Fetch事件到来时,app先进入后台,再执行- (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{}。
判断设备是否开启后台应用程序刷新功能
本文主要教你如何使用iOS 7 SDK多任务处理API--Background Fetch。我们生活在一个社交化的世界中,大部分用户都安装了几个社交类app,但是每次用户打开app,他们必须要等待app加载更新才能看到更新
本文主要教你如何使用iOS 7 SDK多任务处理API--Background Fetch。我们生活在一个社交化的世界中,大部分用户都安装了几个社交类app,但是每次用户打开app,他们必须要等待app加载更新才能看到跟更多最新的内容,对于越来越没耐心的用户来说这一点无疑令人非常痛苦。现在,iOS 7的后台获取(Background Fetch)可以很好地解决这个问题,在用户打开应用之前,app就能自动更新获取内容。
以检测流量的app为例来说明Background Fetch如何工作。如果你会在每天早上查看应用,我们假设在8:20 AM,,你的iOS app必须在当时获得信息。现在如果操作系统知道你将会在8:20 AM左右使用app,那么它可以提前获得数据,从而提供更好的用户体验。
关于iOS 7多任务执行更全面的概览可参看我们的主题“iOS
7 SDK: Multitasking Enhancements”。以下我们将会以一个实例工程来演示如何使用后台获取(Background Fetch)。
1.项目安装
第一步是创建一个iOS 7项目,并选择单视图app,接着添加一些有用的属性:
@property (nonatomic) NSMutableArray *objects; @property (nonatomic) NSArray *possibleTableData; @property (nonatomic) int numberOfnewPosts; @property (nonatomic) UIRefreshControl *refreshControl;
NSMutablearray对象将会被用来在TableView中保存对象列表。在这个教程中,你将不能调用任何服务来获得数据。相反,你将使用possibleTableData数组,并随机从中选择几个对象。整个numberOfnewPosts代表新发布的内容--每次进行请求或者接收后台获取时可用。refrestControl是一个在更新任务时使用的控件。由于不在教程之内,所以本文不会在此展开。
在Main.storyboard中,把ViewController改为UITableViewController,下一步,点击UITableViewController,转到Editor > Embed in > Navigation Controller。记得把自定义类设置为ViewController。然后转至ViewController.m,第一步加载一些数据。以下代码将会申请内存并创建数据对象,创建一个标题以及初始化refreshControl:
self.possibleTableData = [NSArray arrayWithObjects:@"Spicy garlic Lime Chicken",@"Apple Crisp II",@"Eggplant Parmesan II",@"Pumpkin Ginger Cupcakes",@"Easy Lasagna", @"Puttanesca", @"Alfredo Sauce", nil]; self.navigationItem.title = @"Delicious Dishes"; self.refreshControl = [[UIRefreshControl alloc] init]; [self.refreshControl addTarget:self action:@selector(insertNewObject:) forControlEvents:UIControlEventValueChanged]; [self.tableView addSubview:self.refreshControl];
以上代码将会产生一个提醒,因为我们丢失了insertNewObject method。让我们来解决它。该方法将会产生一个随机数,并且将从日期数组获得对象相同的数据,然后它将会通过新值来更新tableview。
- (void)insertNewObject:(id)sender { self.numberOfnewPosts = [self getRandomNumberBetween:0 to:4]; NSLog(@"%d new fetched objects",self.numberOfnewPosts); for(int i = 0; i < self.numberOfnewPosts; i++){ int addPost = [self getRandomNumberBetween:0 to:(int)([self.possibleTableData count]-1)]; [self insertObject:[self.possibleTableData objectAtIndex:addPost]]; } [self.refreshControl endRefreshing]; }
当你添加以下方法时,getRandomNumberBetween提醒将会被禁止:
-(int)getRandomNumberBetween:(int)from to:(int)to { return (int)from + arc4random() % (to-from+1); }
为了在 NSArray object上加载对象,我们需要执行TableView委托函数。
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.objects.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; cell.textLabel.text = self.objects[indexPath.row]; if(indexPath.row < self.numberOfnewPosts){ cell.backgroundColor = [UIColor yellowColor]; } else cell.backgroundColor = [UIColor whiteColor]; return cell; }
非常简单吧?如果运行项目,你会看到一个类似下图的界面:
2. Background Fetch
现在开始创建Background Fetch功能,首先从Project开始,接着是Capabilities,然后Put Background Modes ON,再选择Background Fetch,如下图所示:
但仅仅做这个是不够的。默认地,app不会调用后台API,所以你需要在AppDelegate.m文件中把以下代码添加至-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions method.
[[UIApplication sharedApplication] setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];
这个可以让系统决定何时应该展示新内容。现在你的app已经知道启动ackground fetch,让我们告诉它要做些什么。方法-(void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler将会对你有所帮助。每当执行后台获取时该方法都会被调用,并且应该被包含在AppDelegate.m文件中。以下是完整版本:
-(void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { UINavigationController *navigationController = (UINavigationController*)self.window.rootViewController; id topViewController = navigationController.topViewController; if ([topViewController isKindOfClass:[ViewController class]]) { [(ViewController*)topViewController insertNewObjectForFetchWithCompletionHandler:completionHandler]; } else { NSLog(@"Not the right class %@.", [topViewController class]); completionHandler(UIBackgroundFetchResultFailed); } }
下一步你应该也把ViewController头文件放进AppDelegate.m类。
#import "ViewController.h"
注意insertNewObjectForFetchWithCompletionHandler并没有被创建,所以还需要在ViewController.h中声明它。
- (void)insertNewObjectForFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler;
现在关注执行文件,类似于之前insertNewObject调用的添加。我们使用completionHandler来和系统“交流”,并让它告诉我们app是否现在获取数据,或者当前是否有有效数据。
- (void)insertNewObjectForFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { NSLog(@"Update the tableview."); self.numberOfnewPosts = [self getRandomNumberBetween:0 to:4]; NSLog(@"%d new fetched objects",self.numberOfnewPosts); for(int i = 0; i < self.numberOfnewPosts; i++){ int addPost = [self getRandomNumberBetween:0 to:(int)([self.possibleTableData count]-1)]; [self insertObject:[self.possibleTableData objectAtIndex:addPost]]; } /* At the end of the fetch, invoke the completion handler. */ completionHandler(UIBackgroundFetchResultNewData); }
完成代码,现在我们模拟一个测试,并验证所有项目都能启动和运行。
3. Simulated Background Fetch
如果想确定是否每件事都已经配置好了,你需要编辑Schemes,在Schemes列表点击Manage Schemes选项,如下:
在Schemes管理区你可以复制app的scheme:
复制后scheme会在新窗口展示。你可在Options标签下更改它的名称。选择“Launch due to a background fetch event”框,并在所有窗口中点击“OK”。
接着,使用复制的scheme运行app。注意app不会在前台打开,但是它应该已经获得了一些内容。如果打开app,并且几个recipe已生效,那就说明操作已经成功了。为了使用后台获取功能,你也可以从Xcode菜单的Debug > Simulate Background Fetch开始。
************************************************
转自:http://www.tekuba.net/program/320/
开启Background Fetch支持
在XCode->TARGETS->Capabilities->Background Modes打开并添加Background Fetch.
同时在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions方法中添加:
[[UIApplication sharedApplication] setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];
MinimumBackgroundFetchInterval参数值是两次Fetch时间间隔,不能保证每隔这个时间间隔都会调用。这里设置为UIApplicationBackgroundFetchIntervalMinimum,意思是尽可能频繁的调用我们的Fetch方法。
增加实现Fetch方法
- (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{};
每次系统Fetch时都会调用该方法,我们可以在该方法中做刷新数据等操作,操作执行完成以后要调用completionHandlerblock(),比如:completionHandler(UIBackgroundFetchResultNewData);文档中说系统会根据completionHandler(执行的时间)来估计此次Fetch的耗电等。如果耗时耗电比较多,可能会降低被调用的次数。但这个方法也不是不限时执行的,说是有30s的时间来执行操作。completionHandler有三个参数:
UIBackgroundFetchResultNewData 成功拉取数据
UIBackgroundFetchResultNoData 没有新数据
UIBackgroundFetchResultFailed 拉取数据失败或者超时
模拟Fetch事件
在实际的IOS7环境中,Fetch事件是由系统管理的,app开发者无法预先知道Fetch事件达到的时机。但XCode也提供了Fetch事件的调试办法,在XCode上运行程序后,在Debug->Simulate Background Fetch.
还有一种情况是app没有运行(不在前台也不在后台),被Fetch事件唤醒执行.这种情况的测试方法如下:
Product->Scheme->Edit scheme 在Debug模式选中Options,点选Launch due to a background fetch event,运行即可。
]可以观察到当Fetch事件到来时,app先进入后台,再执行- (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{}。
判断设备是否开启后台应用程序刷新功能
UIAlertView * alertView = [[UIAlertViewalloc]initWithTitle:nil message:@"你没有开启后台刷新,请在设置-》通用-》应用程序后台刷新中开启" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alertView show];
相关文章推荐
- 前端学习笔记之页面制作(三)——HTML
- 前端学习笔记之页面制作(二)——开发环境Sublime Text2(持续更新)
- NodeJS npm常用命令
- ios coredata sqlite3 NSFetchedResultsController(2)
- 巨廉为您读书 - Mobile Access Safety Beyond BYOD (十一)
- jquery html5 上传图片并且 图片回显
- JSONP
- jquery实现checkbox复选框的全选反选
- JS简介
- 表单JS提交失效
- has offers 文档翻译
- js操作json对象
- dom和json使用response 返回
- 关于BufferedWriter.write超过30W条数据写入过慢问题。
- Neo4j: Create multiple relationships between the same two nodes
- 解决json_encode中文UNICODE转码问题
- SoftReference
- angular.element方法汇总(转)以及AngularJS 动态添加元素和删除元素
- 【grunt整合版】30分钟学会使用grunt打包前端代码
- Js-加密