objective-c Reachability class

安稳与你 提交于 2019-12-13 04:32:36

问题


I am trying to have my app determine if a user has a internet connection and what type of connection they have. I've imported the SystemConnection framework and the Reachability .h and .m files.

In my viewController.h I have the following:

#import "Reachability.h"

Reachability* reachability;

and in vc.m

//notification for network status change
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNetworkChange:) name:kReachabilityChangedNotification object:nil];

[[NSNotificationCenter defaultCenter] postNotificationName:kReachabilityChangedNotification object:nil];

[reachability startNotifier];


//check connectivity
[self checkConnectivity];

in checkConnectivity:

- (void) checkConnectivity {

    NetworkStatus remoteHostStatus = [reachability currentReachabilityStatus];

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

}

this works fine on start up. I log the progress and it comes back as expected:

2013-07-29 09:35:17.084 OAI_Project_Template[6095:c07] not connected - network change
2013-07-29 09:35:17.093 OAI_Project_Template[6095:c07] wifi- check connectity 

However, if I turn my wifi connection on or off handleNetworkChange is never called.

- (void) handleNetworkChange : (NSNotification*) notification {

    NetworkStatus remoteHostStatus = [reachability currentReachabilityStatus];

    if(remoteHostStatus == NotReachable) {
    isConnected = NO;
    NSLog(@"not connected - network change");
    } else if (remoteHostStatus == ReachableViaWiFi) {
    NSLog(@"wifi - network change");
    } else if (remoteHostStatus == ReachableViaWWAN) {
    NSLog(@"cell");
    }
}

I've looked around on SO and see a lot of similar problems but the solutions all seem to be set up as I have it.

I'm working in the simulator if that matters. Any help would be appreciated.


回答1:


https://github.com/tonymillion/Reachability is a custom replacement for the apple reachability class




回答2:


Here is what I have to detect network. Basically, all I need is always to detect if the app has access to a server in internet and once the notification is posted, check access and handle depending on the access. Make sure you are registering for reachability only when the app is in foreground.

// Called from applicationDidBecomeActive

- (void) startMonitoring
{
    //Register for change in reachability   
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];

    // Setup a target server to detect if a host on the internet can be accessed    . For example www.apple.com. Defined as instance variable
        hostReach = [Reachability reachabilityWithHostName: @"www.apple.com"];
        [hostReach startNotifier];
}

- (void)reachabilityChanged:(NSNotification *)note
{
    //NSLog(@"%s %@", __FUNCTION__, note);
    Reachability* curReach = [note object];
    NSParameterAssert([curReach isKindOfClass: [Reachability class]]);

    [self updateInterfaceWithReachability: curReach];

}
- (void)updateInterfaceWithReachability: (Reachability*) curReach

{


// Check if the host site is online
NetworkStatus hostStatus = [hostReach currentReachabilityStatus];
switch (hostStatus)
{
    case NotReachable:
    {
        NSLog(@"%s No access - ", __FUNCTION__);

        break;
    }
    case ReachableViaWiFi:
    {
        // Check for LAN switch
        NSLog(@"%s WIFI Available - ", __FUNCTION__);
        break;
    }
    case ReachableViaWWAN:
    {
        // Disable LAN switch
        NSLog(@"%s WIFI NOT Available ", __FUNCTION__);

        break;
    }
 }
}


来源:https://stackoverflow.com/questions/17926026/objective-c-reachability-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!