user4679035
user4679035

Reputation:

How to test internet checking inside AppDelegate?

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.

Upvotes: 0

Views: 700

Answers (2)

SleepNot
SleepNot

Reputation: 3038

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

Upvotes: 0

Bevin Patel
Bevin Patel

Reputation: 302

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

Upvotes: 1

Related Questions