[英]How to check network status in iphone app?
I have setup some methods to check network status in my app. 我已经设置了一些方法来检查我的应用程序中的网络状态。
In my viewDidLoad
method I call initNetworkCheck
: 在我的
viewDidLoad
方法中,我调用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.
问题是它总是给我NO,但是当我记录它时,internetActive实际上是YES。
//[[[[[[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. 我忘了确切的位置,但是在SDK的某个地方有一个例子。
EDIT: looks like Nick found it... :) 编辑:看起来像尼克发现它... :)
I suggest to make use of Apple's Reachability class. 我建议使用Apple的Reachability类。 Here is a sample App by Apple.
这是Apple的App示例。
The Reachability sample application demonstrates how to use the SystemConfiguration framework to monitor the network state of an iPhone or iPod touch.
Reachability示例应用程序演示了如何使用SystemConfiguration框架来监视iPhone或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.
特别是,它演示了如何知道何时可以路由IP以及何时通过无线广域网(WWAN)接口(如EDGE或3G)路由流量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.