[objective-c]互联网连接回来时-IOS 重试请求

标签: ios Objective-C
发布时间: 2016/10/2 9:49:35
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我使用 AFNetworking 3.0 在我的应用程序中执行 Web 请求。 有没有办法要回来的互联网时自动重试请求吗?

这是请求代码︰

        @try {
            NSString *urlMuniByGov = [NSString stringWithFormat:@"%@/%@", URL_MUNICIPALITES, selectedGov.govID];
            NSURL *url = [NSURL URLWithString:urlMuniByGov];
            AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] init];
            manager.responseSerializer = [AFJSONResponseSerializer serializer];
            manager.securityPolicy.allowInvalidCertificates = YES;
           [manager GET:url.absoluteString
             parameters:nil
             progress:nil
             success:^(NSURLSessionDataTask * task, id responseObject) {

                 NSArray *muniNSArray = [responseObject objectForKey:@"municipalites"];
                 if ([muniNSArray isKindOfClass:[NSArray class]]){
                     for (NSDictionary *dictionary in muniNSArray) {
                         Municipality *munModel = [Municipality new] ;
                         munModel.munID = [dictionary objectForKey:@"id"];
                         munModel.munNameAr = [[dictionary objectForKey:@"nom"] objectForKey:@"ar"];
                         munModel.munNameFr = [[dictionary objectForKey:@"nom"] objectForKey:@"fr"];
                         [self.munsArray addObject:munModel];
                         [self.munsString addObject:munModel.munNameAr];
                     }
                 }
                 [municipalityText setItemList:[NSArray arrayWithArray:self.munsString]];
             } failure:^(NSURLSessionDataTask * task, NSError * error) {
                 NSLog(@"Error: %@", error);
             }];
        }
        @catch (NSException *exception) {
           NSLog(@"Exception: %@", exception);
        }

解决方法 1:

[[AFNetworkReachabilityManager sharedManager]setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
NSLog(@"Reachability: %@", AFStringFromNetworkReachabilityStatus(status));}];

如果在网络连接此块中的任何更改将调用,所以在这里你可以重试请求额外信息跟随链接https://github.com/AFNetworking/AFNetworking#network-reachability-manager

官方微信
官方QQ群
31647020