0

我正在尝试NSURLSessionDataTask在后台获取时在我的应用程序委托中运行一个,但是尽管我从完成处理程序中获取了有效数据,但应用程序并未触发该方法。这是我的代码

 - (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];

}
4

1 回答 1

0

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

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

于 2014-11-28T11:53:16.410 回答