Background FetchではNSURLConnectionは使用できないのでNSURLSessionを使用する
・ダウンロード結果を受け取るクラスを作成
MyDownloadDelegate.h
1 2 3 4 5 6 7 8 |
#import <Foundation/Foundation.h> @interface MyDownloadDelegate : NSObject <NSURLSessionDataDelegate> - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didResumeAtOffset:(int64_t)fileOffset expectedTotalBytes:(int64_t)expectedTotalBytes; - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite; - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location; @end |
MyDownloadDelegate.m
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#import "MyDownloadDelegate.h" @implementation MyDownloadDelegate - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location { NSData *data = [NSData dataWithContentsOfURL:location]; NSString *identifier = session.configuration.identifier; NSString *contents = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; NSLog(@"Downloaded!id=%@:%@",identifier, contents); } - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didResumeAtOffset:(int64_t)fileOffset expectedTotalBytes:(int64_t)expectedTotalBytes { } -(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite { } |
AppDelegate.m
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { //background fetchが呼ばれる最短の間隔 [[UIApplication sharedApplication] setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];//最短 return YES; } - (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { //処理 NSString *identifier; NSURLSessionConfiguration *conf; NSURLSession *session; NSURL *requestURL; NSMutableURLRequest *request; NSData *data; NSURLSessionDownloadTask *task; MyDownloadDelegate *downloadDelegate = [[MyDownloadDelegate alloc] init]; //GET identifier = @"BackgroundFetchSessionGet"; conf = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:identifier]; session = [NSURLSession sessionWithConfiguration:conf delegate:downloadDelegate delegateQueue:nil]; requestURL = [NSURL URLWithString:@"URL"]; request = [NSMutableURLRequest requestWithURL:requestURL]; request.HTTPMethod = @"GET"; task = [session downloadTaskWithRequest:request]; [task resume];//処理開始 //POST identifier = @"BackgroundFetchSessionPost"; conf = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:identifier]; session = [NSURLSession sessionWithConfiguration:conf delegate:downloadDelegate delegateQueue:nil]; requestURL = [NSURL URLWithString:@"URL"]; data = [@"a=111&b=222" dataUsingEncoding:NSUTF8StringEncoding]; request = [NSMutableURLRequest requestWithURL:requestURL]; request.HTTPMethod = @"POST"; request.HTTPBody = data; task = [session downloadTaskWithRequest:request]; [task resume];//処理開始 //処理後に呼ぶ //UIBackgroundFetchResultNewData 更新成功 //UIBackgroundFetchResultNoData 更新なし //UIBackgroundFetchResultFailed エラー completionHandler(UIBackgroundFetchResultNewData); } |