检测运营商连接types(3G / EDGE / GPRS)
我如何获得运营商networking的连接types?
- 我能够得到如果连接是使用
Reachability
类的WIFI或WWAN -
我能够获得networking标志
可达性标志状态:WR t —— localWiFiStatusForFlags
-
我能够使用
CaptiveNetwork
获得WIFI SSID
支持的接口:(en0)
en0 => { BSSID = "xx:xx:xx:xx:xx:xx"; SSID = MyWifiNetwork; SSIDDATA = <x1x1x1x1 x1x1x1x1 x1>; }
但我无法区分3G,EDGE或GPRS连接。
任何想法也使用iOS私人API?
谢谢。
从iOS 7上你可以使用:
CTTelephonyNetworkInfo *telephonyInfo = [CTTelephonyNetworkInfo new]; NSLog(@"Current Radio Access Technology: %@", telephonyInfo.currentRadioAccessTechnology); [NSNotificationCenter.defaultCenter addObserverForName:CTRadioAccessTechnologyDidChangeNotification object:nil queue:nil usingBlock:^(NSNotification *note) { NSLog(@"New Radio Access Technology: %@", telephonyInfo.currentRadioAccessTechnology); }];
我也发现这可以检测到一个缓慢或快速的连接:
- (BOOL)isFast:(NSString*)radioAccessTechnology { if ([radioAccessTechnology isEqualToString:CTRadioAccessTechnologyGPRS]) { return NO; } else if ([radioAccessTechnology isEqualToString:CTRadioAccessTechnologyEdge]) { return NO; } else if ([radioAccessTechnology isEqualToString:CTRadioAccessTechnologyWCDMA]) { return YES; } else if ([radioAccessTechnology isEqualToString:CTRadioAccessTechnologyHSDPA]) { return YES; } else if ([radioAccessTechnology isEqualToString:CTRadioAccessTechnologyHSUPA]) { return YES; } else if ([radioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMA1x]) { return NO; } else if ([radioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMAEVDORev0]) { return YES; } else if ([radioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMAEVDORevA]) { return YES; } else if ([radioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMAEVDORevB]) { return YES; } else if ([radioAccessTechnology isEqualToString:CTRadioAccessTechnologyeHRPD]) { return YES; } else if ([radioAccessTechnology isEqualToString:CTRadioAccessTechnologyLTE]) { return YES; } return YES; }
这里的OLD
解决scheme使用私有API ,特别是SoftwareUpdateServices.framework
Class NetworkMonitor = NSClassFromString(@"SUNetworkMonitor"); NSLog(@"TYPE: %d", [NetworkMonitor currentNetworkType]);
它返回:
0:没有数据
1:WIFI
2:GPRS / EDGE
3:3G
希望这有助于社区。
接受的答案不适用于iOS 10.我发现一个解决方法,并在AppDelegate中设置一个计时器,每隔5秒检查currentRadioAccessTechnology属性。 因此,我们还需要一个function来检查WIFI连接是否可用,而不是无线电接入技术。
检查WIFI连接是否可用:
class func isConnectedToWlan() -> Bool { var zeroAddress = sockaddr_in(sin_len: 0, sin_family: 0, sin_port: 0, sin_addr: in_addr(s_addr: 0), sin_zero: (0, 0, 0, 0, 0, 0, 0, 0)) zeroAddress.sin_len = UInt8(MemoryLayout.size(ofValue: zeroAddress)) zeroAddress.sin_family = sa_family_t(AF_INET) let defaultRouteReachability = withUnsafePointer(to: &zeroAddress) { $0.withMemoryRebound(to: sockaddr.self, capacity: 1) {zeroSockAddress in SCNetworkReachabilityCreateWithAddress(nil, zeroSockAddress) } } var flags: SCNetworkReachabilityFlags = SCNetworkReachabilityFlags(rawValue: 0) if SCNetworkReachabilityGetFlags(defaultRouteReachability!, &flags) == false { return false } //Only Working for WIFI let isReachable = flags == .reachable let needsConnection = flags == .connectionRequired return isReachable && !needsConnection }
像这样设置计时器:
Timer.scheduledTimer(timeInterval: TimeInterval.seconds(5.0), target: self, selector: #selector(showNetworkMessage), userInfo: nil, repeats: true)
每5秒调用一次的select器:
guard !Reachability.isConnecteToWlan() else { //Connected to WLAN return } guard let currentRadioAccessTechnology = info.currentRadioAccessTechnology else { // No internet connection return } guard (currentRadioAccessTechnology == CTRadioAccessTechnologyGPRS || currentRadioAccessTechnology == CTRadioAccessTechnologyEdge) else { // 3G, LTE fast radio access Technology return } if lastRadioAccessTechnology != nil { guard let lastRadioAccessTechnology = lastRadioAccessTechnology, (lastInfo != currentRadioAccessTechnology || lastInfo != currentRadioAccessTechnology) else { //Internet connection did not change return } } // Internet connection changed to Edge or GPRS // Store lastRadioAccessTechnology to check if internet connection changed lastRadioAccessTechnology = currentRadioAccessTechnology
我正在开发一个iPhone应用程序,它需要能够识别当前正在使用哪种types的互联网连接(Wifi,3G,Edge等)。 我发现了一个简单的方法来检查使用苹果可达性示例代码。 似乎有关于这个在线信息短缺,我希望这可以帮助某人。
首先将Reachability.m / .h复制到您的项目中,并在您的课程中包含#include“Reachability.h”。
Reachability *reach = [[Reachability alloc]init]; if (reach.internetConnectionStatus == NotReachable) { NSLog(@"No Connection Found"); } else if (reach.internetConnectionStatus == ReachableViaCarrierDataNetwork) { NSLog(@"3G or Edge"); } else if (reach.internetConnectionStatus == ReachableViaWiFiNetwork) { NSLog(@"Wifi Connection"); } [reach release];
这个代码可能不是实现这个目标的最好方法,但它似乎是最简单的方法。