iOS 检测 3G 或 WiFi
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7938650/
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
iOS Detect 3G or WiFi
提问by jwknz
I am not sure if this is possible, but I have this scenario.
我不确定这是否可能,但我有这种情况。
I have a website displayed in my UIWebView which has the link set in a UISegmentedController. They website can detect if you are on wifi or on the 3g network.
我在 UIWebView 中显示了一个网站,该网站在 UISegmentedController 中设置了链接。他们的网站可以检测您是在 wifi 上还是在 3g 网络上。
Now the segmented controller points to 2 different pages: 1 - An iPhone friendly login screen 2 - The home page, once you are logged in.
现在分段控制器指向 2 个不同的页面:1 - 一个 iPhone 友好的登录屏幕 2 - 主页,一旦你登录。
Now here is the question:
现在问题来了:
Can I program my application to detect whether it is to WIFI or 3G (I know you can do this), but then based on the answer go to segment 1 or 2
我可以对我的应用程序进行编程以检测它是 WIFI 还是 3G(我知道你可以这样做),然后根据答案转到第 1 段或第 2 段
Kind of like this:
有点像这样:
if (iPhone device is on 3g) {
Go to Segment 1;
} else {
Go to Segment 0;
}
回答by James Webster
Using the code that Apple has provided here
使用 Apple 在此处提供的代码
Reachability *reachability = [Reachability reachabilityForInternetConnection];
[reachability startNotifier];
NetworkStatus status = [reachability currentReachabilityStatus];
if(status == NotReachable)
{
//No internet
}
else if (status == ReachableViaWiFi)
{
//WiFi
}
else if (status == ReachableViaWWAN)
{
//3G
}
回答by Pavel Alexeev
If you don't want to import Reachability library or deal with notifiers, you can use this simple synchronous method:
如果不想导入 Reachability 库或处理通知程序,可以使用这种简单的同步方法:
typedef enum {
ConnectionTypeUnknown,
ConnectionTypeNone,
ConnectionType3G,
ConnectionTypeWiFi
} ConnectionType;
+ (ConnectionType)connectionType
{
SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithName(NULL, "8.8.8.8");
SCNetworkReachabilityFlags flags;
BOOL success = SCNetworkReachabilityGetFlags(reachability, &flags);
CFRelease(reachability);
if (!success) {
return ConnectionTypeUnknown;
}
BOOL isReachable = ((flags & kSCNetworkReachabilityFlagsReachable) != 0);
BOOL needsConnection = ((flags & kSCNetworkReachabilityFlagsConnectionRequired) != 0);
BOOL isNetworkReachable = (isReachable && !needsConnection);
if (!isNetworkReachable) {
return ConnectionTypeNone;
} else if ((flags & kSCNetworkReachabilityFlagsIsWWAN) != 0) {
return ConnectionType3G;
} else {
return ConnectionTypeWiFi;
}
}
回答by Zaid Pathan
Import Apple's Reachabilityand try this,
导入 Apple 的Reachability并尝试这个,
#import "Reachability.h"
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
//Try this
Reachability *reachability = [Reachability reachabilityForInternetConnection];
[reachability startNotifier];
NetworkStatus status = [reachability currentReachabilityStatus];
if(status == NotReachable)
{
NSLog(@"none");
//No internet
}
else if (status == ReachableViaWiFi)
{
NSLog(@"Wifi");
//WiFi
}
else if (status == ReachableViaWWAN)
{
NSLog(@"WWAN");
//connection type
CTTelephonyNetworkInfo *netinfo = [[CTTelephonyNetworkInfo alloc] init];
_carrier = [[netinfo subscriberCellularProvider] carrierName];
if ([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyGPRS]) {
NSLog(@"2G");
} else if ([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyEdge]) {
NSLog(@"2G");
} else if ([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyWCDMA]) {
NSLog(@"3G");
} else if ([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyHSDPA]) {
NSLog(@"3G");
} else if ([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyHSUPA]) {
NSLog(@"3G");
} else if ([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMA1x]) {
NSLog(@"2G");
} else if ([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMAEVDORev0]) {
NSLog(@"3G");
} else if ([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMAEVDORevA]) {
NSLog(@"3G");
} else if ([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMAEVDORevB]) {
NSLog(@"3G");
} else if ([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyeHRPD]) {
NSLog(@"3G");
} else if ([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyLTE]) {
NSLog(@"4G");
}
}
References(Links may broke in future) :
参考资料(链接将来可能会中断):
回答by Geri Borbás
I made a pretty simple block based Reachability wrapper that strips all the outdated C-like Reachability code, poured into a much more Cocoa form.
我制作了一个非常简单的基于块的 Reachability 包装器,它去除了所有过时的类似 C 的 Reachability 代码,注入了更多的 Cocoa 形式。
Usage like:
用法如:
[EPPZReachability reachHost:hostNameOrIPaddress
completition:^(EPPZReachability *reachability)
{
if (reachability.reachableViaCellular) [self doSomeLightweightStuff];
}];
See Reachability with blocks for everyday useat eppz!blog, or grab it directly from eppz!reachability at GitHub.
见可达性与日常使用的块在eppz!博客,或直接从抓住它在GitHub上eppz!可达性。
It also works with IP addresses, which turned out to be a pretty rare Reachability wrapper feature.
它也适用于 IP 地址,结果证明这是一个非常罕见的可达性包装器功能。
回答by Sachin Agarwal
For swift we can use:
对于 swift 我们可以使用:
func getNetworkType()->String {
do{
let reachability:Reachability = try Reachability.reachabilityForInternetConnection()
do{
try reachability.startNotifier()
let status = reachability.currentReachabilityStatus
if(status == .NotReachable){
return ""
}else if (status == .ReachableViaWiFi){
return "Wifi"
}else if (status == .ReachableViaWWAN){
let networkInfo = CTTelephonyNetworkInfo()
let carrierType = networkInfo.currentRadioAccessTechnology
switch carrierType{
case CTRadioAccessTechnologyGPRS?,CTRadioAccessTechnologyEdge?,CTRadioAccessTechnologyCDMA1x?: return "2G"
case CTRadioAccessTechnologyWCDMA?,CTRadioAccessTechnologyHSDPA?,CTRadioAccessTechnologyHSUPA?,CTRadioAccessTechnologyCDMAEVDORev0?,CTRadioAccessTechnologyCDMAEVDORevA?,CTRadioAccessTechnologyCDMAEVDORevB?,CTRadioAccessTechnologyeHRPD?: return "3G"
case CTRadioAccessTechnologyLTE?: return "4G"
default: return ""
}
// Get carrier name
}else{
return ""
}
}catch{
return ""
}
}catch{
return ""
}
}
回答by Ely
When using iOS 12 or newer, you can use NWPathMonitor
instead of the pre-historic Reachability
class :
使用 iOS 12 或更新版本时,您可以使用NWPathMonitor
代替史前Reachability
类:
import Network // Put this on top of your class
let monitor = NWPathMonitor()
monitor.pathUpdateHandler = { path in
if path.status != .satisfied {
// Not connected
}
else if path.usesInterfaceType(.cellular) {
// Cellular 3/4/5g connection
}
else if path.usesInterfaceType(.wifi) {
// Wi-fi connection
}
else if path.usesInterfaceType(.wiredEthernet) {
// Ethernet connection
}
}
monitor.start(queue: DispatchQueue.global(qos: .background))
回答by HDdeveloper
Class method is as follow
类方法如下
+(NSString*)connectedNetworkType {
Reachability *reachability = [Reachability reachabilityForInternetConnection];
[reachability startNotifier];
NetworkStatus status = [reachability currentReachabilityStatus];
if(status == NotReachable) {
NSLog(@"none");
//No internet
}
else if (status == ReachableViaWiFi) {
NSLog(@"Wifi");
//WiFi
return @"Wifi";
}
else if (status == ReachableViaWWAN){
NSLog(@"WWAN");
//connection type
CTTelephonyNetworkInfo *netinfo = [[CTTelephonyNetworkInfo alloc] init];
// _carrier = [[netinfo subscriberCellularProvider] carrierName];
if (([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyGPRS])
||([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyEdge])
||([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMA1x])) {
NSLog(@"2G");
return @"2G";
}
else if (([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyWCDMA])
||([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyHSDPA])
||([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyHSUPA])
||([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMAEVDORev0])
||([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMAEVDORevA])
||([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMAEVDORevB])
||([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyeHRPD])){
NSLog(@"3G");
return @"3G";
}
else if ([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyLTE]) {
NSLog(@"4G");
return @"4G";
}
}
return @"-1";//default unknown
}
回答by Hogdotmac
#import <ifaddrs.h>
#import <arpa/inet.h>
BOOL CheckWiFi() {
struct ifaddrs *interfaces = NULL;
struct ifaddrs *temp_addr = NULL;
BOOL hasWifi = NO;
int err = getifaddrs(&interfaces);
if(err == 0) {
temp_addr = interfaces;
while(temp_addr) {
if(temp_addr->ifa_addr->sa_family == AF_INET) {
struct sockaddr_in *addr = (struct sockaddr_in *)temp_addr->ifa_addr;
if(memcmp(temp_addr->ifa_name, "en", 2) == 0) {
hasWifi = YES;
break;
}
}
temp_addr = temp_addr->ifa_next;
}
}
freeifaddrs(interfaces);
return hasWifi;
}
To check if you are in a wifi, this saves the costly check of making a connection. Check for ifa_name "bridge" to check for internet sharing.
要检查您是否在 wifi 中,这可以节省昂贵的连接检查。检查 ifa_name "bridge" 以检查互联网共享。
回答by orafaelreis
If you are using Xamarin or Monotouch you can use Reachability adapted class from Xamarin GitHub repository:
如果您使用的是 Xamarin 或 Monotouch,则可以使用 Xamarin GitHub 存储库中的 Reachability 适配类:
https://github.com/xamarin/monotouch-samples/blob/master/ReachabilitySample/reachability.cs
https://github.com/xamarin/monotouch-samples/blob/master/ReachabilitySample/reachability.cs
So add it to your project and call Reachability.InternetConnectionStatus()
所以将它添加到您的项目中并调用 Reachability.InternetConnectionStatus()
回答by Etienne Bley
Here is an updated version for iOS 6 with SimplePing from apple also included. It is ARC compatible and I started from another person's fix to Reachability. http://elbsolutions.com/projects/reachability-with-simpleping-wrapper/
这是 iOS 6 的更新版本,还包括来自 Apple 的 SimplePing。它与 ARC 兼容,我从另一个人对 Reachability 的修复开始。 http://elbsolutions.com/projects/reachability-with-simpleping-wrapper/
I hope this helps someone.
我希望这可以帮助别人。