开发者

How to check network status in iphone app?

开发者 https://www.devze.com 2023-02-25 17:40 出处:网络
I have setup some methods to check network status in my app. In my viewDidLoad method I call initNetworkCheck:

I have setup some methods to check network status in my app.

In my viewDidLoad method I call initNetworkCheck:

[self initNetworkCheck];
[super viewDidLoad];
if(internetActive == NO){     
    compose.enabled = NO;
}

So I want to check on startup if the hardware has internet connection. The problem is it gives me always NO but internetActive is actually YES when I log it.

//[[[[[[network check methods
-(void)checkNetworkStatus:(NSNotification *)notice{
    NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
    switch (internetStatus) {
        case NotReachable:{
            self.internetActive = NO; 
            break;
        }
  开发者_运维百科      case ReachableViaWiFi:{
            self.internetActive = YES;
            break;
        }
        case ReachableViaWWAN:{
            self.internetActive = YES;
            break;
        }
    }
    NetworkStatus hostStatus = [hostReachable currentReachabilityStatus];
    switch (hostStatus){
        case NotReachable:{
            self.hostActive = NO;
            break;
        }
        case ReachableViaWiFi:{
            self.hostActive = YES;
            break;
        }
        case ReachableViaWWAN:{
            self.hostActive = YES;
            break;
        }
    }

}
-(void)initNetworkCheck{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetworkStatus:) name:kReachabilityChangedNotification object:nil];

    internetReachable = [[Reachability reachabilityForInternetConnection] retain];
    [internetReachable startNotifier];

    hostReachable = [[Reachability reachabilityWithHostName:@"www.google.com"] retain];
    [hostReachable startNotifier];
}
//]]]]]]

Any ideas?


I personally use the following:

typedef enum
{
    NoConnection = 0,
    WiFiConnected,
    WWANConnected
} NetworkStatus;

NetworkStatus getNetworkStatus ( )
{
    struct sockaddr_in nullAddress;

    bzero(&nullAddress, sizeof(nullAddress));
    nullAddress.sin_len = sizeof(nullAddress);
    nullAddress.sin_family = AF_INET;

    SCNetworkReachabilityRef ref = SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault, (const struct sockaddr*) &nullAddress);

    SCNetworkReachabilityFlags flags;
    SCNetworkReachabilityGetFlags(ref, &flags);

    if (!(flags & kSCNetworkReachabilityFlagsReachable))
        return NoConnection;

    if (!(flags & kSCNetworkReachabilityFlagsConnectionRequired))
        return WiFiConnected;

    if (((flags & kSCNetworkReachabilityFlagsConnectionOnDemand) ||
        (flags & kSCNetworkReachabilityFlagsConnectionOnTraffic)) &&
        !(flags & kSCNetworkReachabilityFlagsInterventionRequired))
        return WiFiConnected;

    if ((flags & kSCNetworkReachabilityFlagsIsWWAN) == kSCNetworkReachabilityFlagsIsWWAN)
        return WWANConnected;

    return NoConnection;
}

I forget exactly where, but there's an example in the SDK somewhere that this is based on.

EDIT: looks like Nick found it... :)


I suggest to make use of Apple's Reachability class. Here is a sample App by Apple.

The Reachability sample application demonstrates how to use the SystemConfiguration framework to monitor the network state of an iPhone or iPod touch. In particular, it demonstrates how to know when IP can be routed and when traffic will be routed through a Wireless Wide Area Network (WWAN) interface such as EDGE or 3G.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号