1

我正在使用 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);
        }
4

1 回答 1

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

如果网络连接发生任何变化,此块将调用,因此您可以在此处重试请求以获取更多信息,请点击链接https://github.com/AFNetworking/AFNetworking#network-reachability-manager

于 2016-03-22T16:28:44.063 回答