0

I am developing universal application.Here my app is opened at that time i am checking internet connection is there or not.That purpose i am added AFNETWORKING framework.The problem is while i am debugging my code at that time [manager GET:APIscript parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) this function is not executing the control goes outside.Here my requirement is internet connection is not available means its showing pop up "Please Connect internet"like that Internet is connected means its open normally.Below i am posted my code base

NSURL *baseURL = [NSURL URLWithString:@"https://www.google.co.in"];

    NSString *APIscript=@"/webhp";
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithBaseURL:baseURL];
  manager.responseSerializer = [AFJSONResponseSerializer serializer];
    [manager.requestSerializer setTimeoutInterval:20.0];
    [manager GET:APIscript parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        if ([task.response isKindOfClass:[NSHTTPURLResponse class]]) {
            NSHTTPURLResponse *r = (NSHTTPURLResponse *)task.response;
            if ([r statusCode] == 200) {
            //Do success stuff 
            }
}

             failure:^(NSURLSessionDataTask *task, NSError *error) {
                 UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
                 [alertView show];



                 //do failure stuff

             }];

Here This line(manager GET:APIscript parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) ) only not executing.please guide me any one to resolve this issue.Thanks in advance.

  • Here my main requirement is some times internet checking is taking long time, that time i gave timeout for 10 or 20 sec.In reachability it is not possible right@Anbu.Karthik –  Mar 18 '15 at 07:08
  • [Check this](http://stackoverflow.com/questions/8812459/easiest-way-to-detect-internet-connection-on-ios) – Bista Mar 18 '15 at 07:08
  • [This one is interesting](http://stackoverflow.com/questions/10105276/how-to-define-a-reachability-timeout-on-ios) – Bista Mar 18 '15 at 07:16
  • Try Apple's Reachability Sample. https://developer.apple.com/library/ios/samplecode/Reachability/Listings/Reachability_Reachability_h.html – SleepNot Mar 18 '15 at 07:20

2 Answers2

1

Download the rateability lib from Github and code this...

GitHub lib

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
   [self setUpRechability];
}

Call this Method in didFinishLaunchingWithOptions in appdelegate

-(void)setUpRechability
{
  [[NSNotificationCenter defaultCenter] addObserver:self
                                           selector:@selector(handleNetworkChange:)
                                               name:kReachabilityChangedNotification
                                             object:nil];
  netReachability = [Reachability reachabilityForInternetConnection];
  [netReachability startNotifier];
  [self handleNetworkChange:nil];
 }

This Method will invoke when internet state change..

- (void) handleNetworkChange:(NSNotification *)notice
{
  NetworkStatus remoteHostStatus = [netReachability currentReachabilityStatus];

  if          (remoteHostStatus == NotReachable)      {NSLog(@"no");      hasInet=NO;   }
  else if     (remoteHostStatus == ReachableViaWiFi)  {NSLog(@"wifi");    hasInet=YES;  }
  else if     (remoteHostStatus == ReachableViaWWAN)  {NSLog(@"cell");    hasInet=YES;  }


  if (!hasInet)
  {
      [HUD hide:YES];
      HUD=nil;
  }
}
Bevin Patel
  • 292
  • 3
  • 11
0

You can try Apple's Reachability Sample OR you can make use of AFHTTPRequestOperationManager which is much simpler and easier.

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:@"http://example.com/resources.json" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];
SleepNot
  • 2,812
  • 9
  • 41
  • 68
  • But here control is not going to inside of this function([manager GET:@"http://example.com/resources.json" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject)) –  Mar 18 '15 at 07:26
  • You need to check if it is failing and what the error is. Log the error.localizedDescription to understand it better. – SleepNot Mar 18 '15 at 07:34