ios - NSUrlConnection 未通过后台获取调用

标签 ios background nsurlconnection fetch

 -(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/

相关文章:

ios - 在 iOS 上安装 react-native-firebase 的问题

python - 以分离模式在 IPython Notebook 中运行一些任务

ios - 在 segControl & bars 上自定义 UIButton 或 UIBarButtonItem 的背景

php - 如何使用 NSURLConnection 获得 PHP 回显?

ios - NSURLConnection sendAsynchronousRequest 从不释放内存

ios - 在 iOS 中从 GMT 转换时间表现异常(反向)

ios - 游戏中心 GKLeaderboard 初始化程序失败?

ios - 当应用程序处于后台甚至被用户终止时,在 iOS 上无需移动即可获取用户位置

objective-c - 检查 SSL 证书的身份

iphone - NSMutableURLRequest 在大约 15 秒后超时。超时设置为 240 秒