App Delegate中的NSURLSessionDataTask

时间:2014-11-28 09:30:16

标签: ios appdelegate nsurlsession nsurlsessiondatatask

我正在尝试在后台提取中在我的app委托中运行NSURLSessionDataTask,但是虽然我从完成处理程序获得了有效数据,但app并没有触发该方法。这是我的代码

 - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {

    NSURLSession *serverConfigure = [NSURLSessionConfiguration defaultSessionConfiguration];
    NSURLSessionConfiguration *serverSession = [NSURLSession sessionWithConfiguration:serverConfigure delegate:self delegateQueue:nil];

    NSURLSessionDataTask *serverDownload = [serverSession dataTaskWithURL:[NSURL URLWithString:@"myurl"] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        NSArray *downloadedData = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

        [self handleData:downloadedData];

    }];

    [serverDownload resume];

}

1 个答案:

答案 0 :(得分:0)

在backgroundFetch中,您的应用程序将在后台调用此方法 - (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void(^)(UIBackgroundFetchResult))completionHandler {

为此你必须激活后台获取模式 ...只需浏览此链接http://www.appcoda.com/ios7-background-fetch-programming/