swift 使用swift获取数据使用情况

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了swift 使用swift获取数据使用情况相关的知识,希望对你有一定的参考价值。

    #include <ifaddrs.h>
    
    // http://stackoverflow.com/questions/25626117/how-to-get-ip-address-in-swift
    func getIFAddresses() -> [String] {
        var addresses = [String]()
        
        // Get list of all interfaces on the local machine:
        var ifaddr : UnsafeMutablePointer<ifaddrs> = nil
        if getifaddrs(&ifaddr) == 0 {
            
            // For each interface ...
            for (var ptr = ifaddr; ptr != nil; ptr = ptr.memory.ifa_next) {
                let flags = Int32(ptr.memory.ifa_flags)
                var addr = ptr.memory.ifa_addr.memory
                
                // Check for running IPv4, IPv6 interfaces. Skip the loopback interface.
                if (flags & (IFF_UP|IFF_RUNNING|IFF_LOOPBACK)) == (IFF_UP|IFF_RUNNING) {
                    if addr.sa_family == UInt8(AF_INET) || addr.sa_family == UInt8(AF_INET6) {
                        
                        // Convert interface address to a human readable string:
                        var hostname = [CChar](count: Int(NI_MAXHOST), repeatedValue: 0)
                        if (getnameinfo(&addr, socklen_t(addr.sa_len), &hostname, socklen_t(hostname.count),
                            nil, socklen_t(0), NI_NUMERICHOST) == 0) {
                                if let address = String.fromCString(hostname) {
                                    addresses.append(address)
                                }
                        }
                    }
                }
            }
            freeifaddrs(ifaddr)
        }
        return addresses
    }
    
    // https://github.com/Weirdln/WBIphone/blob/master/Util/SystemInfoFunc/SystemInfoFunc.m
    func getDataUsage() -> [UInt32] {
        var ifaddr : UnsafeMutablePointer<ifaddrs> = nil
        var networkData: UnsafeMutablePointer<if_data>! = nil
    
        var wifiDataSent:UInt32 = 0
        var wifiDataReceived:UInt32 = 0
        var wwanDataSent:UInt32 = 0
        var wwanDataReceived:UInt32 = 0
    
        if getifaddrs(&ifaddr) == 0 {
            for (var ptr = ifaddr; ptr != nil; ptr = ptr.memory.ifa_next) {
            
                let name = String.fromCString(ptr.memory.ifa_name)
                let flags = Int32(ptr.memory.ifa_flags)
                var addr = ptr.memory.ifa_addr.memory
                
                if addr.sa_family == UInt8(AF_LINK) {
                    if name?.hasPrefix("en") == true {
                        networkData = unsafeBitCast(ptr.memory.ifa_data, UnsafeMutablePointer<if_data>.self)
                        wifiDataSent += networkData.memory.ifi_obytes
                        wifiDataReceived += networkData.memory.ifi_ibytes
                    }
                    
                    if name?.hasPrefix("pdp_ip") == true {
                        networkData = unsafeBitCast(ptr.memory.ifa_data, UnsafeMutablePointer<if_data>.self)
                        wwanDataSent += networkData.memory.ifi_obytes
                        wwanDataReceived += networkData.memory.ifi_ibytes
                    }
                }
            }
            freeifaddrs(ifaddr)
        }
        
        return [wifiDataSent, wifiDataReceived, wwanDataSent, wwanDataReceived]
    }

以上是关于swift 使用swift获取数据使用情况的主要内容,如果未能解决你的问题,请参考以下文章

如何在没有 microsoft azure 用户的情况下使用 swift 获取访问令牌?

使用 swift 在不使用自动尺寸的情况下获取 tableView 中动态文本视图的高度

Swift字典获取值的关键

iOS (Swift):使用关系获取核心数据

Swift - 保存并稍后获取大量数据的最佳方式

使用 Swift 4 和 Xcode 9 获取 JSON 数据