如果任务因网络故障而挂起,iOS NSURLSession将恢复该任务

如果任务因网络故障而挂起,iOS NSURLSession将恢复该任务,ios,xcode,nsurlsession,Ios,Xcode,Nsurlsession,我使用NSURLSessionDataTask从服务器获取数据。我的问题是,如果从服务器接收数据时网络出现故障,那么服务器将停止。因此,当网络正常工作时,我如何恢复任务。我使用的是网络可达性API,但我遗漏了一些内容 下面是在我的项目中使用的示例代码: //Add the Observer for checking the internet connections [[NSNotificationCenter defaultCenter] addObserver:self sel

我使用NSURLSessionDataTask从服务器获取数据。我的问题是,如果从服务器接收数据时网络出现故障,那么服务器将停止。因此,当网络正常工作时,我如何恢复任务。我使用的是网络可达性API,但我遗漏了一些内容

下面是在我的项目中使用的示例代码:

    //Add the Observer for checking the internet connections
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
self.internetReachability = [Reachability reachabilityForInternetConnection];
[self.internetReachability startNotifier];

self.wifiReachability = [Reachability reachabilityForLocalWiFi];
[self.wifiReachability startNotifier];

//Starts the downloading the data....
+(void)downloadDataFromURL:(NSURL *)url withCompletionHandler:(void (^)(NSData *))completionHandler{

// Instantiate a session configuration object.
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];

// Instantiate a session object.
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration];

// Create a data task object to perform the data downloading.
NSURLSessionDataTask *task = [session dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

    if (error != nil) {
        // If any error occurs then just display its description on the console.
        NSLog(@"error %@", [error localizedDescription]);
    }
    else{
        // If no error occurs, check the HTTP status code.
        NSInteger HTTPStatusCode = [(NSHTTPURLResponse *)response statusCode];

        // If it's other than 200, then show it on the console.
        if (HTTPStatusCode != 200) {
            NSLog(@"HTTP status code = %ld", (long)HTTPStatusCode);
        }

        // Call the completion handler with the returned data on the main thread.
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            completionHandler(data);
        }];
    }
}];

// Resume the task.
[task resume];
我正在调用通知API来检查网络

-(void) reachabilityChanged:(NSNotification *)note
{
Reachability* curReach = [note object];
[self updateInterfaceWithReachability:curReach];
}

- (void)updateInterfaceWithReachability:(Reachability *)reachability
{
  if (reachability == self.internetReachability)
 {
    [self updateInternetActivity:reachability];
 }

  if (reachability == self.wifiReachability)
 {
    [self updateInternetActivity:reachability];

 }
}

- (void)updateInternetActivity:(Reachability *)reachability
{
 NetworkStatus netStatus = [reachability currentReachabilityStatus];
 switch (netStatus)
{
    case NotReachable:        {
        [task suspend];
        break;
    }

    case ReachableViaWWAN:        {
        [task resume];

        break;
    }
    case ReachableViaWiFi:        {
        [task resume];

        break;
    }
}

}

我正在恢复任务,但它不起作用。请帮忙,提前谢谢。

大家好,找到解决方案了吗??