Track cellular data usage using swift

前端 未结 1 1766
予麋鹿
予麋鹿 2020-12-31 17:56

Is there any way to track cellular data used by device in swift? Is there any api? I am trying to achieve something like this

相关标签:
1条回答
  • 2020-12-31 18:27

    Updated to Swift 4 you can use this easy extension. To track the device cell and wifi Usage.

    extension SystemDataUsage {
    
        public static var wifiCompelete: UInt64 {
            return SystemDataUsage.getDataUsage().wifiSent + SystemDataUsage.getDataUsage().wifiReceived
        }
    
        public static var wwanCompelete: UInt64 {
            return SystemDataUsage.getDataUsage().wirelessWanDataSent + SystemDataUsage.getDataUsage().wirelessWanDataReceived
        }
    
    }
    
    class SystemDataUsage {
    
        private static let wwanInterfacePrefix = "pdp_ip"
        private static let wifiInterfacePrefix = "en"
    
        class func getDataUsage() -> DataUsageInfo {
            var ifaddr: UnsafeMutablePointer<ifaddrs>?
            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<ifaddrs>) -> 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<ifaddrs>, name: String) -> DataUsageInfo {
            var networkData: UnsafeMutablePointer<if_data>?
            var dataUsageInfo = DataUsageInfo()
    
            if name.hasPrefix(wifiInterfacePrefix) {
                networkData = unsafeBitCast(pointer.pointee.ifa_data, to: UnsafeMutablePointer<if_data>.self)
                if let data = networkData {
                    dataUsageInfo.wifiSent += UInt64(data.pointee.ifi_obytes)
                    dataUsageInfo.wifiReceived += UInt64(data.pointee.ifi_ibytes)
                }
    
            } else if name.hasPrefix(wwanInterfacePrefix) {
                networkData = unsafeBitCast(pointer.pointee.ifa_data, to: UnsafeMutablePointer<if_data>.self)
                if let data = networkData {
                    dataUsageInfo.wirelessWanDataSent += UInt64(data.pointee.ifi_obytes)
                    dataUsageInfo.wirelessWanDataReceived += UInt64(data.pointee.ifi_ibytes)
                }
            }
            return dataUsageInfo
        }
    }
    
    struct DataUsageInfo {
        var wifiReceived: UInt64 = 0
        var wifiSent: UInt64 = 0
        var wirelessWanDataReceived: UInt64 = 0
        var wirelessWanDataSent: UInt64 = 0
    
        mutating func updateInfoByAdding(_ info: DataUsageInfo) {
            wifiSent += info.wifiSent
            wifiReceived += info.wifiReceived
            wirelessWanDataSent += info.wirelessWanDataSent
            wirelessWanDataReceived += info.wirelessWanDataReceived
        }
    }
    

    You can use it like: SystemDataUsage.wifiCompelete

    0 讨论(0)
提交回复
热议问题