互联网连接恢复时重试请求 - IOS

时间:2016-03-22 16:02:57

标签: ios objective-c afnetworking-3

我正在使用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 个答案:

答案 0 :(得分:4)

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

如果此块的网络连接有任何变化,那么你可以在这里重试一个请求 有关其他信息,请点击https://github.com/AFNetworking/AFNetworking#network-reachability-manager

链接