iOS 7 SDK: 如何使用后台获取(Background Fetch)

本文主要教你如何使用iOS 7 SDK多任务处理API--Background Fetch。咱们生活在一个社交化的世界中,大部分用户都安装了几个社交类app,可是每次用户打开app,他们必需要等待app加载更新才能看到跟更多最新的内容,对于愈来愈没耐心的用户来讲这一点无疑使人很是痛苦。如今,iOS 7的后台获取(Background Fetch)能够很好地解决这个问题,在用户打开应用以前,app就能自动更新获取内容。 ios

 
以检测流量的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: app

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。 dom

- (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提醒将会被禁止: iphone

-(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是否如今获取数据,或者当前是否有有效数据。 fetch

- (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开始。
 
源文件下载:
/cms/uploads/soft/131113/4673-131113193430.zip
 
来源: mobile.tutsplus
相关文章
相关标签/搜索