Error Domain=NSURLErrorDomain Code=-1005 “The network connection was lost.”

前端 未结 30 3463
悲哀的现实
悲哀的现实 2020-11-22 05:51

I have an application which works fine on Xcode6-Beta1 and Xcode6-Beta2 with both iOS7 and iOS8. But with Xcode6-Beta3, Beta4, Beta5 I\'m facing network issues with iOS8 but

30条回答
  •  醉梦人生
    2020-11-22 06:34

    See pjebs comment on Jan 5 on Github.

    Method1 :

    if (error.code == -1005)
    {
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
    
            dispatch_group_t downloadGroup = dispatch_group_create();
            dispatch_group_enter(downloadGroup);
            dispatch_group_wait(downloadGroup, dispatch_time(DISPATCH_TIME_NOW, 5000000000)); // Wait 5 seconds before trying again.
            dispatch_group_leave(downloadGroup);
            dispatch_async(dispatch_get_main_queue(), ^{
                //Main Queue stuff here
                [self redoRequest]; //Redo the function that made the Request.
            });
        });
    
        return;
    }
    

    Also some suggests to re-connect to the site,

    i.e. Firing the POST request TWICE

    Solution: Use a method to do connection to the site, return (id), if the network connection was lost, return to use the same method.

    Method 2

    -(id) connectionSitePost:(NSString *) postSender Url:(NSString *) URL {
         // here set NSMutableURLRequest =>  Request
    
        NSHTTPURLResponse *UrlResponse = nil;
        NSData *ResponseData = [[NSData alloc] init];
    
        ResponseData = [NSURLConnection sendSynchronousRequest:Request returningResponse:&UrlResponse error:&ErrorReturn];
    
         if ([UrlResponse statusCode] != 200) {
    
              if ([UrlResponse statusCode] == 0) {
    
                      /**** here re-use method ****/
                      return [self connectionSitePost: postSender Url: URL];
              }
    
         } else {
              return ResponseData;
         }
    
    }
    

提交回复
热议问题