-(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 个 if block 都没有执行。我该如何实现?我需要通过后台获取下载数据。请帮助... NSUrlSession
也不起作用..
最佳答案
您是否尝试发送同步请求?我认为您的 performFetchWithCompletionHandler 方法在 NSURLConnection 收到响应之前完成。顺便问一下,你确定调用了 performFetchWithCompletionHandler 吗?也许您需要为其设置功能?
关于ios - NSUrlConnection 未通过后台获取调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33214989/