NSURLConnection的经由背景不叫取

问题描述:

-(void)application:(UIApplication)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{ 

NSLog(@"In performFetchWithCompletionHandler"); 
DownloadAlerts alertDownload = [[DownloadAlerts alloc]init]; 
[alertDownload getAlertsOnLocUpdate]; 
completionHandler(UIBackgroundFetchResultNewData); // We will add content here soon. 
} 

[NSURLConnection sendAsynchronousRequest:request 
             queue:[[NSOperationQueue alloc] init] 
          completionHandler:^(NSURLResponse *response, 
               NSData *data, 
               NSError *error) { 

           if ([data length] >0 && error == nil) { 
            NSLog(@"Hello01"); 

           } else if ([data length] == 0 && error == nil) { 
            NSLog(@"Hello02"); 

           } else if (error != nil) { 
            NSLog(@"Hello03"); 
           } 
          }]; 

我执行上面的代码段在其被在appdelegate.h经由performFetchWithCompletionHandler调用的函数。 当我启动的应用程序通过后台抓取,没有3块是否得到执行。我如何实现这一点?我需要通过后台获取下载数据。好心帮... NSUrlSession也好好尝试一下工作..NSURLConnection的经由背景不叫取

+0

是你的要求后为零? –

+0

也发布你在代码中如何调用它的代码 –

+0

//appdelegate.m - (void)application:(UIApplication *)application performFetchWithCompletionHandler :(void(^)(UIBackgroundFetchResult))completionHandler Download * download = [[下载alloc] init]; [下载getData]; completionHandler(UIBackgroundFetchResultNewData);} //该功能可以获取调用上面已经给出......而且没有要求不是零... – Akshara

你试图发送同步请求?我认为你的``performFetchWithCompletionHandler方法在NSURLConnection接收到响应之前完成。顺便说一句,你确定performFetchWithCompletionHandler被调用?也许你需要为它设置功能?

需要调用 “completionHandler(UIBackgroundFetchResultNewData);”你接收到数据后,将其传递给“getAlertsOnLocUpdate”的方法作为参数,并调用它收到来自服务器的回答(中的URLConnection的completionHandler)