iOS 有关WIFI讯息获取
前几天碰到一个需求,是有关于监测App内获取网络权限的需求,由于之前没碰过这种需求 ,只能面向google编程,网上搜到的资料都是关于OC的。
需求用到的类大部分是基于C,需要运用到大部分指针方面的写法。对于长期使用swift开发的人来说,写起来可能会感觉到很陌生。网上有关这种需求的写法的文章也不多,我只能自己一行一行地翻译成swift。 在这里记录一下。
import SystemConfiguration.CaptiveNetwork
import CoreTelephony
流量监测
//流量监测
func getInterfaceBytes() -> UInt32 {
var addrsPointer : UnsafeMutablePointer<ifaddrs>? = nil
var inBytes : UInt32 = 0
var outBytes : UInt32 = 0
if getifaddrs(&addrsPointer) == 0 {
var pointer = addrsPointer
while pointer != nil {
if let addrs = pointer?.pointee {
let name = String(cString: addrs.ifa_name)
if addrs.ifa_addr.pointee.sa_family == UInt8(AF_LINK) {
if name.hasPrefix("en") { // Wifi
let networkData = unsafeBitCast(addrs.ifa_data, to: UnsafeMutablePointer<if_data>.self)
inBytes += networkData.pointee.ifi_ibytes
outBytes += networkData.pointee.ifi_obytes
} else if name.hasPrefix("pdp_ip") { // WWAN
let networkData = unsafeBitCast(addrs.ifa_data, to: UnsafeMutablePointer<if_data>.self)
inBytes += networkData.pointee.ifi_ibytes
outBytes += networkData.pointee.ifi_obytes
}
}
}
pointer = pointer?.pointee.ifa_next
}
freeifaddrs(addrsPointer)
}
return inBytes
}
获取wifi地址
func getWiFiAddress() -> String? {
var address : String?
// Get list of all interfaces on the local machine:
var ifaddr : UnsafeMutablePointer<ifaddrs>? = nil
if getifaddrs(&ifaddr) == 0 {
// For each interface ...
var ptr = ifaddr
while ptr != nil {
}
freeifaddrs(ifaddr)
}
return address
}
获取wifi名称
//获取wifi名称
func fetchSSIDInfo() -> String {
var currentSSID = "WifiNameIsNil"
if let interfaces:CFArray = CNCopySupportedInterfaces() {
for i in 0..<CFArrayGetCount(interfaces){
let interfaceName: UnsafeRawPointer = CFArrayGetValueAtIndex(interfaces, i)
let rec = unsafeBitCast(interfaceName, to: AnyObject.self)
let unsafeInterfaceData = CNCopyCurrentNetworkInfo("\(rec)" as CFString)
if unsafeInterfaceData != nil {
let interfaceData = unsafeInterfaceData! as Dictionary!
currentSSID = interfaceData![NSString(string: "SSID")] as! String
}
}
}
return currentSSID
}
从手机状态栏获取当前网络状态
//从状态栏获取网络状态
func getNetworkTypeFromStatusBar() -> String {
var type : Int = 0
let app = UIApplication.shared
let statusBar : UIView = app.value(forKeyPath: "statusBar") as! UIView
if (statusBar is NSNull){
return "Unknow";
}
if statusBar.isKind(of: NSClassFromString("UIStatusBar_Modern")!) {
// 在 iPhone X 上 statusBar 属于 UIStatusBar_Modern ,需要特殊处理
let currentData = statusBar.value(forKeyPath: "statusBar.currentData") as! NSObject
// BOOL wifiEnable = [[currentData valueForKeyPath:@"_wifiEntry.isEnabled"] boolValue];
let wifiEnable = currentData.value(forKeyPath: "_wifiEntry.isEnabled") as! Bool
// // 这里不能用 _cellularEntry.isEnabled 来判断,该值即使关闭仍然有是 YES
// BOOL cellularEnable = [[currentData valueForKeyPath:@"_cellularEntry.type"] boolValue];
return wifiEnable ? "WiFi" : "CellularData"
// cellularEnable ? ZYNetworkTypeCellularData : ZYNetworkTypeOffline;
} else {
// 传统的状态栏
let children = (statusBar.value(forKeyPath: "foregroundView") as! UIView).subviews
for child in children {
if (child.isKind(of: NSClassFromString("UIStatusBarDataNetworkItemView")!)) {
type = child.value(forKeyPath: "dataNetworkType") as! Int
return type == 5 ? "WiFi" : "CellularData"
}
}
}
return ""
}
网友评论