本文主要教你如何使用iOS 7 SDK多任务处理API--Background Fetch。我们生活在一个社交化的世界中,大部分用户都安装了几个社交类app,但是每次用户打开app,他们必须要等待app加载更新才能看到跟更多最新的内容,对于越来越没耐心的用户来说这一点无疑令人非常痛苦。现在,iOS 7的后台获取(Background Fetch)可以很好地解决这个问题,在用户打开应用之前,app就能自动更新获取内容。
1 @property (nonatomic) NSMutableArray *objects; 2 @property (nonatomic) NSArray *possibleTableData; 3 @property (nonatomic) int numberOfnewPosts; 4 @property (nonatomic) UIRefreshControl *refreshControl;
1 self.possibleTableData = [NSArray arrayWithObjects:@"Spicy garlic Lime Chicken",@"Apple Crisp II",@"Eggplant Parmesan II",@"Pumpkin Ginger Cupcakes",@"Easy Lasagna", @"Puttanesca", @"Alfredo Sauce", nil]; 2 self.navigationItem.title = @"Delicious Dishes"; 3 self.refreshControl = [[UIRefreshControl alloc] init]; 4 [self.refreshControl addTarget:self action:@selector(insertNewObject:) forControlEvents:UIControlEventValueChanged]; 5 [self.tableView addSubview:self.refreshControl];
1 - (void)insertNewObject:(id)sender 2 { 3 self.numberOfnewPosts = [self getRandomNumberBetween:0 to:4]; 4 NSLog(@"%d new fetched objects",self.numberOfnewPosts); 5 for(int i = 0; i < self.numberOfnewPosts; i++){ 6 int addPost = [self getRandomNumberBetween:0 to:(int)([self.possibleTableData count]-1)]; 7 [self insertObject:[self.possibleTableData objectAtIndex:addPost]]; 8 } 9 [self.refreshControl endRefreshing]; 10 }
1 -(int)getRandomNumberBetween:(int)from to:(int)to { 2 return (int)from + arc4random() % (to-from+1); 3 }
1 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 2 return 1; 3 } 4 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 5 return self.objects.count; 6 } 7 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 8 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; 9 cell.textLabel.text = self.objects[indexPath.row]; 10 if(indexPath.row < self.numberOfnewPosts){ 11 cell.backgroundColor = [UIColor yellowColor]; 12 } 13 else 14 cell.backgroundColor = [UIColor whiteColor]; 15 return cell; 16 }


1 [[UIApplication sharedApplication] setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum]; 
MinimumBackgroundFetchInterval参数值是时间间隔的数值,系统保证两次Fetch的时间间隔不会小于这个值,不能保证每隔这个时间间隔都会调用。这里设置为UIApplicationBackgroundFetchIntervalMinimum,意思是告诉系统,尽可能频繁的调用我们的Fetch方法。现在你的app已经知道启动ackground 
fetch,让我们告诉它要做些什么。方法-(void)application:(UIApplication 
*)application performFetchWithCompletionHandler:(void 
(^)(UIBackgroundFetchResult))completionHandler将会对你有所帮助。每当执行后台获取时该方法都会被调用,并且应该被包含在AppDelegate.m文件中。以下是完整版本:1 -(void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { 2 UINavigationController *navigationController = (UINavigationController*)self.window.rootViewController; 3 id topViewController = navigationController.topViewController; 4 if ([topViewController isKindOfClass:[ViewController class]]) { 5 [(ViewController*)topViewController insertNewObjectForFetchWithCompletionHandler:completionHandler]; 6 } else { 7 NSLog(@"Not the right class %@.", [topViewController class]); 8 completionHandler(UIBackgroundFetchResultFailed); 9 } 10 }
1 #import "ViewController.h"
1 - (void)insertNewObjectForFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler;
1 - (void)insertNewObjectForFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { 2 NSLog(@"Update the tableview."); 3 self.numberOfnewPosts = [self getRandomNumberBetween:0 to:4]; 4 NSLog(@"%d new fetched objects",self.numberOfnewPosts); 5 for(int i = 0; i < self.numberOfnewPosts; i++){ 6 int addPost = [self getRandomNumberBetween:0 to:(int)([self.possibleTableData count]-1)]; 7 [self insertObject:[self.possibleTableData objectAtIndex:addPost]]; 8 } 9 /* 10 At the end of the fetch, invoke the completion handler. 11 */ 12 completionHandler(UIBackgroundFetchResultNewData); 13 }
直接运行程序,在Xcode的菜单中,选择”Debug” -> “Simulate Background 
Fetch”,你会发现会先打开App,然后后台挂起,接着执行(void)application: 
performFetchWithCompletionHandler方法。

复制(Duplicate)一份当前的Schema,在新的Schema的Options下,选中”Launch due to a background fetch event”,运行这个Schema。


原文:http://www.cnblogs.com/easy-coding/p/3564940.html