Ted*_*y13 5 iphone objective-c internet-connection reachability
我想检查用户是否有活跃的互联网连接.这就是我实现它的方式.它似乎工作正常,但问题是它总是显示我的iPhone模拟器(uialert出现)没有连接,即使我的wifi打开或关闭.有谁知道我做错了什么?谢谢你的帮助!
Reachability *r= [Reachability reachabilityWithHostName:@"http://www.google.com"];
NetworkStatus internetStatus= [r currentReachabilityStatus];
if ((internetStatus != ReachableViaWiFi) && (internetStatus != ReachableViaWWAN))
{
UIAlertView *alert= [[UIAlertView alloc] initWithTitle:@"No internet" message:@"No internet connection found. Please try again later"
delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
else {
// execute code in app
}
Run Code Online (Sandbox Code Playgroud)
这就是我在我的应用程序中完成的方式:
Reachability *reachability = [Reachability reachabilityForInternetConnection];
NetworkStatus internetStatus = [reachability currentReachabilityStatus];
if(internetStatus == NotReachable) {
UIAlertView *errorView;
errorView = [[UIAlertView alloc]
initWithTitle: NSLocalizedString(@"Network error", @"Network error")
message: NSLocalizedString(@"No internet connection found, this application requires an internet connection to gather the data required.", @"Network error")
delegate: self
cancelButtonTitle: NSLocalizedString(@"Close", @"Network error") otherButtonTitles: nil];
[errorView show];
[errorView autorelease];
}
Run Code Online (Sandbox Code Playgroud)
它的作用是检查互联网连接,而不是它是否可以到达域.如果没有互联网连接(wifi或celluar),它将显示UIAlertView消息(本地化).