本文主要教你如何使用iOS 7 SDK多任務處理API--Background Fetch。咱們生活在一個社交化的世界中,大部分用戶都安裝了幾個社交類app,可是每次用戶打開app,他們必需要等待app加載更新才能看到跟更多最新的內容,對於愈來愈沒耐心的用戶來講這一點無疑使人很是痛苦。如今,iOS 7的後臺獲取(Background Fetch)能夠很好地解決這個問題,在用戶打開應用以前,app就能自動更新獲取內容。 ios
第一步是建立一個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: 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; }
但僅僅作這個是不夠的。默認地,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是否如今獲取數據,或者當前是否有有效數據。 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); }
![]() |
/cms/uploads/soft/131113/4673-131113193430.zip |