【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>>
本文主要教你如何使用iOS 7 SDK多任务处理API--Background Fetch。我们生活在一个社交化的世界中,大部分用户都安装了几个社交类app,但是每次用户打开app,他们必须要等待app加载更新才能看到跟更多最新的内容,对于越来越没耐心的用户来说这一点无疑令人非常痛苦。现在,iOS 7的后台获取(Background Fetch)可以很好地解决这个问题,在用户打开应用之前,app就能自动更新获取内容。
第一步是创建一个iOS 7项目,并选择单视图app,接着添加一些有用的属性:
@property (nonatomic) NSMutableArray *objects;
@property (nonatomic) NSArray *possibleTableData;
@property (nonatomic) int numberOfnewPosts;
@property (nonatomic) UIRefreshControl *refreshControl;
在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;
}


但仅仅做这个是不够的。默认地,app不会调用后台API,所以你需要在AppDelegate.m文件中把以下代码添加至-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions method.
[[UIApplication sharedApplication] setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];
-(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);
}
}
#import "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);
}



![]() |
/cms/uploads/soft/131113/4673-131113193430.zip |
来源:oschina
链接:https://my.oschina.net/u/728866/blog/184594