xcode 如何在 iphone 应用程序中检查网络状态?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5662298/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check network status in iphone app?
提问by Upvote
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?
有任何想法吗?
采纳答案by Nick Weaver
I suggest to make use of Apple's Reachability class. Hereis a sample App by Apple.
我建议使用 Apple 的 Reachability 类。这是 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.
Reachability 示例应用程序演示了如何使用 SystemConfiguration 框架来监视 iPhone 或 iPod touch 的网络状态。特别是,它演示了如何知道何时可以路由 IP 以及何时将通过无线广域网 (WWAN) 接口(例如 EDGE 或 3G)路由流量。
回答by Mac
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... :)
编辑:看起来尼克找到了它...... :)