【问题标题】:Is 'performFetchWithCompletionHandler' called when no internet connection?没有互联网连接时是否调用“performFetchWithCompletionHandler”?
【发布时间】:2015-12-09 10:53:58
【问题描述】:

如果设备未连接到互联网,UIApplicationDelegate 会呼叫performFetchWithCompletionHandler 吗?在这种情况下,文档不清楚。

【问题讨论】:

  • performFetchWithCompletionHandler 被系统调用以给您的应用程序一些处理时间来更新自身。它与下载失败无关。你不是指- (void)application:(UIApplication *)application handleEventsForBackgroundURLSession:(NSString *)identifier completionHandler:(void (^)())completionHandler吗?

标签: ios objective-c ios8 ios9 uiapplicationdelegate


【解决方案1】:

经过一些测试,我可以声称如果设备未连接到 Internet,则不会调用 performFetchWithCompletionHandler 委托方法。 在 iOS8 和 iOS9 上测试。

【讨论】:

    【解决方案2】:

    下载完成后不会调用-application:performFetchWithCompletionHandler:。它由系统调用,让您的应用有机会下载数据。您可以根据需要进行正常的错误处理。

    -(void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
        NSURL *URL = // Your URL
        [[[NSURLSession sharedSession] dataTaskWithURL:URL completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
            if (error != nil) {
                // Handle Error
                completionHandler(UIBackgroundFetchResultFailed);
                return;
            }
    
            // Process the data
            completionHandler(UIBackgroundFetchResultNewData);
        }] resume];
    }
    

    【讨论】:

      猜你喜欢
      • 2020-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-23
      • 1970-01-01
      • 1970-01-01
      • 2014-10-10
      相关资源
      最近更新 更多