Swift 4
Sample Usage
let usage = getDataUsage()
// prints '3527660544 bytes of wifi'
print("\(usage.wifi.sent) bytes of wifi")
// prints '3.29 GB of wifi'
let usageString = ByteCountFormatter.string(fromByteCount: Int64(usage.wifi.sent), countStyle: .binary)
print("\(usageString) of wifi")
Code
import Foundation
typealias DataUsage = (wifi: (sent: UInt32, received: UInt32), wwan: (sent: UInt32, received: UInt32))
func getDataUsage() -> DataUsage {
var interfaceAddresses: UnsafeMutablePointer<ifaddrs>?
let status = getifaddrs(&interfaceAddresses)
defer { freeifaddrs(interfaceAddresses) }
var returnData = DataUsage((0, 0), (0, 0))
guard status == 0, let addresses = interfaceAddresses else { return returnData }
for pointer in AddressSequence(interfaceAddresses: addresses) {
guard pointer.pointee.ifa_addr.pointee.sa_family == AF_LINK else { continue }
let networkData = unsafeBitCast(pointer.pointee.ifa_data, to: UnsafeMutablePointer<if_data>.self)
let (bytesIn, bytesOut) = (networkData.pointee.ifi_ibytes, networkData.pointee.ifi_obytes)
let name = String(cString: pointer.pointee.ifa_name)
if name.hasPrefix("en") {
returnData.wifi.sent += bytesOut
returnData.wifi.received += bytesIn
} else if name.hasPrefix("pdp_ip") {
returnData.wwan.sent += bytesOut
returnData.wwan.received += bytesIn
}
}
return returnData
}
class AddressSequence: Sequence {
init(interfaceAddresses: UnsafeMutablePointer<ifaddrs>) {
self.interfaceAddresses = interfaceAddresses
}
let interfaceAddresses: UnsafeMutablePointer<ifaddrs>
typealias Element = UnsafeMutablePointer<ifaddrs>
func makeIterator() -> AddressIterator {
return AddressIterator(currentPointer: interfaceAddresses)
}
}
class AddressIterator: IteratorProtocol {
init(currentPointer: UnsafeMutablePointer<ifaddrs>) {
self.currentPointer = currentPointer
}
var currentPointer: UnsafeMutablePointer<ifaddrs>?
public func next() -> UnsafeMutablePointer<ifaddrs>? {
currentPointer = currentPointer?.pointee.ifa_next ?? nil
return currentPointer
}
}
Old Version
Here's one possible implementation.
First, include ifaddrs
in your Objective-C bridging header:
#include <ifaddrs.h>
Then, try out this function:
func getDataUsage() -> (wifi : (sent : UInt32, received : UInt32), wwan : (sent : UInt32, received : UInt32)) {
var interfaceAddresses : UnsafeMutablePointer<ifaddrs> = nil
var networkData: UnsafeMutablePointer<if_data> = nil
var returnTuple : (wifi : (sent : UInt32, received : UInt32), wwan : (sent : UInt32, received : UInt32)) = ((0, 0), (0, 0))
if getifaddrs(&interfaceAddresses) == 0 {
for var pointer = interfaceAddresses; pointer != nil; pointer = pointer.memory.ifa_next {
let name : String! = String.fromCString(pointer.memory.ifa_name)
println(name);
let flags = Int32(pointer.memory.ifa_flags)
var addr = pointer.memory.ifa_addr.memory
if addr.sa_family == UInt8(AF_LINK) {
if name.hasPrefix("en") {
networkData = unsafeBitCast(pointer.memory.ifa_data, UnsafeMutablePointer<if_data>.self)
returnTuple.wifi.sent += networkData.memory.ifi_obytes
returnTuple.wifi.received += networkData.memory.ifi_ibytes
} else if name.hasPrefix("pdp_ip") {
networkData = unsafeBitCast(pointer.memory.ifa_data, UnsafeMutablePointer<if_data>.self)
returnTuple.wwan.sent += networkData.memory.ifi_obytes
returnTuple.wwan.received += networkData.memory.ifi_ibytes
}
}
}
freeifaddrs(interfaceAddresses)
}
return returnTuple
}
It returns nested Swift tuples representing the four pieces of data you requested. This makes it easy to access. For example:
let usage = getDataUsage()
let wifiDataSentString = "WiFi Data Sent: \(usage.wifi.sent)"