简体   繁体   中英

Internet reachability not working properly in iPhone

I am working on an application which continously tracking internet connectivity and upload some data, app have the feature that when internet is not available it saves data(photos) offline and on internet availability it uploads the data. My app working fine but sometimes its not chechking internet and when i turned off my device wifi and again turn on, It is working, So can anybody please tell me whats wrong here that stuck me? my reachability code is as below:

code

- (void)reachabilityCheck
{
    /* Internet checking  */
    [[NSNotificationCenter defaultCenter] addObserver:self  selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];

    Reachability *reach = [Reachability reachabilityForInternetConnection];
    reach.reachableOnWWAN = YES;
    [reach startNotifier];
    NetworkStatus internetStatus = [reach currentReachabilityStatus];
    if (internetStatus != NotReachable) {
        //Develop By Payal
        if(self.internetConnection == 0)
        {
            NSLog(@"Connection active");
            self.internetConnection = 1;
        }
        //Develop By Payal Done
    }
    else {
        NSLog(@"Connection inactive");
        self.internetConnection = 0;
    }
}

Try this code

Put bellow code in your viewDidLoad method it call the instantly using notification

[[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(checkNetworkStatus:)
                                                 name:kReachabilityChangedNotification object:nil];



//Net Connection Chack
- (void)checkNetworkStatus:(NSNotification *)notice
{
    // called after network status changes

    NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
    switch (internetStatus)
    {
        case NotReachable:
        {
            NSLog(@"The internet is down.");
            UIAlertView *alrt=[[UIAlertView alloc]initWithTitle:@"Error" message:@"No Internet Connection\nPlease Check The Connection" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [alrt show];

            break;
        }
        case ReachableViaWiFi:
        {
            NSLog(@"The internet is working via WIFI");
            break;
        }
        case ReachableViaWWAN:
        {
            NSLog(@"The internet is working via WWAN!");
            break;
        }
    }
}

i hope this usefull you

The problem with your code is that you might not observe any connection change.

I'm not sure what trigger did you use so that your code is working when you turn off and on your wifi connection.

Instead of manually check the connection using timer, you could use Reachability class provided by Apple here .

You could import the class and then register for any connection change in your viewDidLoad (or appDelegate didFinishLaunchingWithOptions method if you want global observer).

//AppDelegate.h
@property (strong, nonatomic) Reachability *reachability;

//AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.reachability = [Reachability reachabilityForInternetConnection];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityDidChange:) name:kReachabilityChangedNotification object:nil];
    [self.reachability startNotifier];
}

#pragma mark - Reachability notification
- (void)reachabilityDidChange:(NSNotification *)notification {
    Reachability *reachability = (Reachability *)[notification object];

    if ([reachability isReachable]) {
        NSLog(@"There is connection");
    } else {
        NSLog(@"Unreachable");
    }
}

Edit:
you define these 2 following scenario to test your code.

  1. it is working in case when i am killing the app ,reopen the app and after that i turn on the wifi
  2. when i first turn on the wifi and then reopen the app,it is not working

In scenario 1, your observer know the connection change since the app is still alive. However, in scenario 2 your app does not have any info on connection change since there are no change after you open your app.

If you want to achieve what you want (upload media to server) in scenario 2, then Reachability change is not the tool that you need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM