iPhone Data Usage Tracking/Monitoring

前端 未结 7 1264
感情败类
感情败类 2020-11-22 04:08

I\'ve searched over this topic but found very few details which were helpful. With these details I\'ve tried to cook some code as follows.

Note: Please compa

7条回答
  •  礼貌的吻别
    2020-11-22 04:35

    I fixed above source code to Swift3 version

    struct DataUsageInfo {
        var wifiReceived: UInt32 = 0
        var wifiSent: UInt32 = 0
        var wirelessWanDataReceived: UInt32 = 0
        var wirelessWanDataSent: UInt32 = 0
    
        mutating func updateInfoByAdding(_ info: DataUsageInfo) {
            wifiSent += info.wifiSent
            wifiReceived += info.wifiReceived
            wirelessWanDataSent += info.wirelessWanDataSent
            wirelessWanDataReceived += info.wirelessWanDataReceived
        }
    }
    
    
    class DataUsage {
    
        private static let wwanInterfacePrefix = "pdp_ip"
        private static let wifiInterfacePrefix = "en"
    
        class func getDataUsage() -> DataUsageInfo {
            var ifaddr: UnsafeMutablePointer?
            var dataUsageInfo = DataUsageInfo()
    
            guard getifaddrs(&ifaddr) == 0 else { return dataUsageInfo }
            while let addr = ifaddr {
                guard let info = getDataUsageInfo(from: addr) else {
                    ifaddr = addr.pointee.ifa_next
                    continue
                }
                dataUsageInfo.updateInfoByAdding(info)
                ifaddr = addr.pointee.ifa_next
            }
    
            freeifaddrs(ifaddr)
    
            return dataUsageInfo
        }
    
        private class func getDataUsageInfo(from infoPointer: UnsafeMutablePointer) -> DataUsageInfo? {
            let pointer = infoPointer
            let name: String! = String(cString: pointer.pointee.ifa_name)
            let addr = pointer.pointee.ifa_addr.pointee
            guard addr.sa_family == UInt8(AF_LINK) else { return nil }
    
            return dataUsageInfo(from: pointer, name: name)
        }
    
        private class func dataUsageInfo(from pointer: UnsafeMutablePointer, name: String) -> DataUsageInfo {
            var networkData: UnsafeMutablePointer?
            var dataUsageInfo = DataUsageInfo()
    
            if name.hasPrefix(wifiInterfacePrefix) {
                networkData = unsafeBitCast(pointer.pointee.ifa_data, to: UnsafeMutablePointer.self)
                if let data = networkData {
                    dataUsageInfo.wifiSent += data.pointee.ifi_obytes
                    dataUsageInfo.wifiReceived += data.pointee.ifi_ibytes
                }
    
            } else if name.hasPrefix(wwanInterfacePrefix) {
                networkData = unsafeBitCast(pointer.pointee.ifa_data, to: UnsafeMutablePointer.self)
                if let data = networkData {
                    dataUsageInfo.wirelessWanDataSent += data.pointee.ifi_obytes
                    dataUsageInfo.wirelessWanDataReceived += data.pointee.ifi_ibytes
                }
            }
    
            return dataUsageInfo
        }
    }
    

提交回复
热议问题