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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 01:56:49  来源:igfitidea点击:

Test Connection over Wifi only iOS

iosxcodenetworkingwifi

提问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

检查 Tony Million 可达性类

There must be a isReachableViaWiFimethod 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?

如何在 iOS 或 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...
    }