我怎样才能承载网络的连接类型?
支持的接口:(EN0)
en0 => {
BSSID = "xx:xx:xx:xx:xx:xx";
SSID = MyWifiNetwork;
SSIDDATA = <x1x1x1x1 x1x1x1x1 x1>;
}
但我不能够differenziate 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
的解决方案, 使用私有API,特别是SoftwareUpdateServices.framework
Class NetworkMonitor = NSClassFromString(@"SUNetworkMonitor");
NSLog(@"TYPE: %d", [NetworkMonitor currentNetworkType]);
它返回:
0:无数据
1:WIFI
2:GPRS / EDGE
3:3G
希望这有助于社区。
接受的答案是不工作在iOS 10.我发现了一个解决办法,并设置在AppDelegate中的计时器被检查房子currentRadioAccessTechnology每5秒。 因此,我们还需要一个函数来检查,如果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秒:
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
我的工作需要认识到其互联网连接的类型,目前正在使用的(WIFI,3G,Edge等)的能力的iPhone应用程序。 我发现了一个简单的方法,通过使用苹果可达示例代码进行检查。 似乎有关于这个网上信息的不足,我希望这可以帮助别人。
首页复印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];
此代码可能无法做到这一点的最好办法,但它似乎是最简单的方法。