我有一个实现的方法,可以在我的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];
}
答案 0 :(得分:2)
你有没有看过Reachability课程?
http://developer.apple.com/library/ios/#samplecode/Reachability/Introduction/Intro.html
如果Apple已经完成了工作,它可能会为您提供更多的灵活性以及为什么要自己动手操作:)希望这会有所帮助!
答案 1 :(得分:2)
检查Tony Million Reachability class
必须有isReachableViaWiFi
方法才能检查无线网络
// 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;
答案 2 :(得分:0)
您是否看过这个类似的问题:
How to check for an active Internet connection on iOS or OSX?
最高投票问题给出了github链接到一个整洁的解决方法,并显示你是否通过wifi或WWAN(手机)激活互联网
答案 3 :(得分:0)
AFNetworking提供检查网络连接的工具 -
AFNetworkReachabilityManager.sharedManager().setReachabilityStatusChangeBlock { (status: AFNetworkReachabilityStatus) -> Void in
// check your connectivity...
}