问题描述
我看到任何有关可达性的帖子,但人们并没有真正给出问题的确切答案。
在我的应用程序中,我使用来自apple的可达性代码,在我的appDelegate中,我使用:
I saw any post about Reachability but people doesn't really give the exact answer to the problem.In my application I use the Reachability code from apple and in my appDelegate I use this:
-(BOOL)checkInternet {
Reachability *reachability = [Reachability reachabilityWithHostName:@"www.google.com"];
NetworkStatus internetStatus = [reachability currentReachabilityStatus];
BOOL internet;
if ((internetStatus != ReachableViaWiFi) && (internetStatus != ReachableViaWWAN)) {
internet = NO;
}else {
internet = YES;
}
return internet;
}
所以问题是即使我有互联网连接,这段代码告诉我我没有。
有谁知道该怎么做才能使这个工作?
So the problem is even if I have an internet connection, this code telling me that I don't have one.Does anyone know what to do to make this working?
谢谢,
推荐答案
您可能应该使用 + [Reachability reachabilityForInternetConnection]
而不是特定名称的可达性(当然除非您实际需要)。
You should probably be using +[Reachability reachabilityForInternetConnection]
rather than reachability for a particular name (unless of course that's what you actually need).
毕竟,当你仍然有可用的互联网连接时,可能会出现各种原因导致特定服务器无法访问。
There could be all manner of reasons that a particular server might not be reachable while you still have a working internet connection, after all.
这就是我的工作:
BOOL hasInet;
Reachability *connectionMonitor = [Reachability reachabilityForInternetConnection];
[[NSNotificationCenter defaultCenter]
addObserver: self
selector: @selector(inetAvailabilityChanged:)
name: kReachabilityChangedNotification
object: connectionMonitor];
hasInet = [connectionMonitor currentReachabilityStatus] != NotReachable;
然后
-(void)inetAvailabilityChanged:(NSNotification *)notice {
Reachability *r = (Reachability *)[notice object];
hasInet = [r currentReachabilityStatus] != NotReachable;
}
对我来说效果很好。
这篇关于Iphone互联网连接(可达性)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!