xcode 仅通过 Wifi 测试连接 iOS
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13087057/
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
Test Connection over Wifi only iOS
提问by Charles Vincent
I have a method implemented thats tests the internet connection in my iOS app. The problem is, I only want the method to test the internet connection over wifi, not cellular. How do I do that? :-) PS I'm using the latest version of Xcode & iOS 6.
我实现了一种方法来测试我的 iOS 应用程序中的互联网连接。问题是,我只想要通过 wifi 测试互联网连接的方法,而不是蜂窝网络。我怎么做?:-) PS 我使用的是最新版本的 Xcode 和 iOS 6。
NSURL *scriptUrl = [NSURL URLWithString:@"http://10.247.245.87/stores/test.html"];
NSData *data = [NSData dataWithContentsOfURL:scriptUrl];
if (data != nil){
UIAlertView *alert = [[UIAlertView alloc]initWithTitle: @"Success"
message: @"You're connected to the server"
delegate: self
cancelButtonTitle: @"Close"
otherButtonTitles:nil];
//Show Alert On The View
[alert show];
}
else {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle: @"Connection Failed"
message: @"Please connect to network and try again"
delegate: self
cancelButtonTitle: @"Close"
otherButtonTitles:nil];
//Show Alert On The View
[alert show];
}
回答by jerrylroberts
Have you looked into the Reachability class?
你有没有研究过 Reachability 类?
http://developer.apple.com/library/ios/#samplecode/Reachability/Introduction/Intro.html
http://developer.apple.com/library/ios/#samplecode/Reachability/Introduction/Intro.html
It will probably provide you with a lot more flexibility and why roll your own if Apple has already done the work :) Hope this helps!
它可能会为您提供更多的灵活性,如果 Apple 已经完成了这项工作,为什么还要自己动手:) 希望这会有所帮助!
回答by u.gen
Check Tony Million Reachability class
There must be a isReachableViaWiFi
method you can check wifi only
必须有一种isReachableViaWiFi
方法可以检查wifi
// allocate a reachability object
Reachability* reach = [Reachability reachabilityWithHostname:@"www.google.com"];
// tell the reachability that we DONT want to be reachable on 3G/EDGE/CDMA
reach.reachableOnWWAN = NO;
回答by MCKapur
Have you looked at this similar question:
你有没有看过这个类似的问题:
How to check for an active Internet connection on iOS or OSX?
The highest up voted question on that gives a github link to a neat workaround and shows you if the internet connection is active via wifi or WWAN (cellular)
最高投票的问题提供了一个简洁的解决方法的 github 链接,并向您显示互联网连接是否通过 wifi 或 WWAN(蜂窝)处于活动状态
回答by Prakash Raj
AFNetworking providing facility to check network connectivity -
AFNetworking 提供检查网络连接的工具 -
AFNetworkReachabilityManager.sharedManager().setReachabilityStatusChangeBlock { (status: AFNetworkReachabilityStatus) -> Void in
// check your connectivity...
}