Haroun SMIDA
Haroun SMIDA

Reputation: 1116

retry request when the internet connection is back - IOS

I am using AFNetworking 3.0 to perform Web request in my application. Is there a way to automatically retry a request when the internet is back?

This is the request code:

        @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);
        }

Upvotes: 1

Views: 640

Answers (1)

Abi
Abi

Reputation: 501

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

if any changes in the net connection this block will call , so here u can retry a request for additional information follow the link https://github.com/AFNetworking/AFNetworking#network-reachability-manager

Upvotes: 4

Related Questions