1

I have run into the strange problem, both, Reachability and NSURLConnection showing error (No internet connection/Network connection lost) after switching from wifi to 3g. Although all apps are running fine after switching to 3g. I double check if mobile data is on for an app and for the device as well. Reinstalling apps it works fine, please help. Here is some reachability code:

-(BOOL)toCheckNetworkStatus
{
internetReachable = [Reachability reachabilityForInternetConnection];
[internetReachable startNotifier];

NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];

if(internetStatus == NotReachable ){
    [self displayToastWithMessage:@"Connection lost!" AndSubtitle:@"Please check your network connection." delay:3.2];
    return  NO;
}
else{
    return YES;
}
}

Code fo NSURLConnection:

    NSError *error;
    NSData *postData = [NSJSONSerialization dataWithJSONObject:dictionary
                                                       options:0
                                                         error:&error];
    if (error)
        NSLog(@"Failure to serialize JSON object %@", error);


    NSURL *url = [NSURL URLWithString:palURL];
    NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url
                                                           cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                       timeoutInterval:TIMEOUTINTERVAL];

    [request setHTTPMethod:@"POST"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];

    [request setHTTPBody:postData];
    [NSURLConnection sendAsynchronousRequest:request
                                       queue:[NSOperationQueue mainQueue]
                           completionHandler:
     ^(NSURLResponse *response, NSData *data, NSError *connectionError)
     {
         if (data)
         {
             NSMutableDictionary *responseDict = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
             NSLog(@"UPLOADED LOCATION RESPONSE = %@",responseDict);
         }
         else
         {
             NSLog(@"connectionError===%@",connectionError);
         }
     }];
Ketan Shinde
  • 1,847
  • 4
  • 18
  • 38

0 Answers0