Upload a file to FTP - swift

I'm trying to upload a file with Swift.
I found this (poorly written) tutorial from Apple for Objective-C:
I tried to implement it in Swift, but got stuck on dereferencing the pointer in the callback function:
func upload(filepath:String, url:String){
do {
let attr:NSDictionary? = try FileManager.default.attributesOfItem(atPath: filepath) as NSDictionary;
if attr == nil { return; }
let size:UInt64? = attr?.fileSize();
if size==nil { return; }
var info = MyStreamInfo(writeStream: CFWriteStreamCreateWithFTPURL(nil, CFURLCreateWithString(nil,url as CFString, nil)) as! CFWriteStream,
readStream: CFReadStreamCreateWithFile(nil, CFURLCreateWithString(nil, filepath as CFString, nil)),
/*proxyDict: CFDictionary(), */fileSize: size!, totalBytesWritten: 0, leftOverByteCount: 0, buffer: UnsafeMutablePointer<UInt8>.allocate(capacity: bSize));
var myContext=CFStreamClientContext(version: 0, info: &info, retain: nil, release: nil, copyDescription: nil);
//myContext.info = info;
CFReadStreamOpen(info.readStream);
CFWriteStreamSetProperty(info.writeStream, CFStreamPropertyKey(kCFStreamPropertyFTPUserName), username as CFTypeRef);
CFWriteStreamSetProperty(info.writeStream, CFStreamPropertyKey(kCFStreamPropertyFTPPassword), password as CFTypeRef);
CFWriteStreamSetProperty(info.writeStream, CFStreamPropertyKey(kCFStreamPropertyFTPProxyHost), hostname as CFTypeRef);
CFWriteStreamSetProperty(info.writeStream, CFStreamPropertyKey(kCFStreamPropertyFTPProxyPort), port as CFTypeRef);
CFWriteStreamSetClient(info.writeStream, CFStreamEventType.canAcceptBytes.rawValue, writeCB as! CFWriteStreamClientCallBack, &myContext);
} catch let error as NSError {
print(error);
}
}
func writeCB(stream:CFWriteStream, event:CFStreamEventType, myptr: UnsafeMutableRawPointer) {
var info:MyStreamInfo;
var totalBytesRead:Int32 = 0;
repeat {
var bytesRead:Int32 = 0;
var bytesWritten:Int32 = 0;
if info.leftOverByteCount>0 {
bytesRead = Int32(info.leftOverByteCount);
} else {
bytesRead = Int32(CFReadStreamRead(info.readStream, info.buffer, bSize));
if (bytesRead < 0){
print("error");
return;
}
totalBytesRead += bytesRead;
}
bytesWritten = Int32(CFWriteStreamWrite(info.writeStream, info.buffer, CFIndex(bytesRead)));
if bytesWritten > 0 {
info.totalBytesWritten += UInt32(bytesWritten);
if bytesWritten < bytesRead {
info.leftOverByteCount = UInt32(bytesRead - bytesWritten);
memmove(info.buffer, info.buffer.advanced(by: Int(bytesWritten)), Int(info.leftOverByteCount));
} else {
info.leftOverByteCount = 0;
}
} else {
if bytesWritten < 0 {
print("error");
break;
}
}
} while(CFWriteStreamCanAcceptBytes(info.writeStream));
}
How can I get the MyStreamInfo object out of the pointer?
Am I doing the rest right?
username, password, etc are of course members of my class that I set elsewhere.

You can use FilesProvider library to deal with FTP. It doesn't use deprecated CFWriteStreamCreateWithFTPURL API and implements FTP protocol from scratch and provides a high level, FileManager like API to deal with FTP/FTPS.
You can see a sample implementation here.

Related

How do I make a custom completion handler?

How do I make a custom completion handler for the below function? This is writing to a websocket via Starscream and I want to receive a response if it isn't nil.
open func write(string: String, completion: (() -> ())? = nil) {
guard isConnected else { return }
dequeueWrite(string.data(using: String.Encoding.utf8)!, code: .textFrame, writeCompletion: completion)
}
and here is deqeueWrite func:
private func dequeueWrite(_ data: Data, code: OpCode, writeCompletion: (() -> ())? = nil) {
let operation = BlockOperation()
operation.addExecutionBlock { [weak self, weak operation] in
//stream isn't ready, let's wait
guard let s = self else { return }
guard let sOperation = operation else { return }
var offset = 2
var firstByte:UInt8 = s.FinMask | code.rawValue
var data = data
if [.textFrame, .binaryFrame].contains(code), let compressor = s.compressionState.compressor {
do {
data = try compressor.compress(data)
if s.compressionState.clientNoContextTakeover {
try compressor.reset()
}
firstByte |= s.RSV1Mask
} catch {
// TODO: report error? We can just send the uncompressed frame.
}
}
let dataLength = data.count
let frame = NSMutableData(capacity: dataLength + s.MaxFrameSize)
let buffer = UnsafeMutableRawPointer(frame!.mutableBytes).assumingMemoryBound(to: UInt8.self)
buffer[0] = firstByte
if dataLength < 126 {
buffer[1] = CUnsignedChar(dataLength)
} else if dataLength <= Int(UInt16.max) {
buffer[1] = 126
WebSocket.writeUint16(buffer, offset: offset, value: UInt16(dataLength))
offset += MemoryLayout<UInt16>.size
} else {
buffer[1] = 127
WebSocket.writeUint64(buffer, offset: offset, value: UInt64(dataLength))
offset += MemoryLayout<UInt64>.size
}
buffer[1] |= s.MaskMask
let maskKey = UnsafeMutablePointer<UInt8>(buffer + offset)
_ = SecRandomCopyBytes(kSecRandomDefault, Int(MemoryLayout<UInt32>.size), maskKey)
offset += MemoryLayout<UInt32>.size
for i in 0..<dataLength {
buffer[offset] = data[i] ^ maskKey[i % MemoryLayout<UInt32>.size]
offset += 1
}
var total = 0
while !sOperation.isCancelled {
let stream = s.stream
let writeBuffer = UnsafeRawPointer(frame!.bytes+total).assumingMemoryBound(to: UInt8.self)
let len = stream.write(data: Data(bytes: writeBuffer, count: offset-total))
if len <= 0 {
var error: Error?
let errCode = InternalErrorCode.outputStreamWriteError.rawValue
error = s.errorWithDetail("output stream error during write", code: errCode)
s.doDisconnect(error)
break
} else {
total += len
}
if total >= offset {
if let queue = self?.callbackQueue, let callback = writeCompletion {
queue.async {
callback()
}
}
break
}
}
}
writeQueue.addOperation(operation)
}
So right now I can call this function like this:
socket.write(string: frameJSONSring) { () -> Void in
}
But I'd like a response in that handler so that I can read the response data (if there is any) from the socket. Apparently I can pass a custom response handler as a parameter when calling:
socket.write(string: frameJSONSring) { (CUSTOM_HANDLER_HERE) -> Void in
}
open func write(string: String, completion: ((Int) -> ())?) {
guard isConnected else { return }
let someParameter = 5
dequeueWrite(string.data(using: String.Encoding.utf8)!, code: .textFrame, writeCompletion: completion(someParameter))
}
Notice I:
added an Int as a parameter you pass to the handler.
changed completion to completion(someParameter)
You can then use it like such:
socket.write(string: frameJSONSring) { number in
print(number)
}
You can replace the Int with any other type you like.
Also no need to do = nil. When something is an optional then it's already defaulted to nil.

What is the fastest Char-By-Char-Reader in Swift

i have to write a real fast char by char reader in swift. This is my solution so far.
For a 1.4mb file i get it in 0m0.932s. For a 150mb file it took 1m42.931s
Do you know a faster solution?
import Foundation
class CharReader {
let encoding : String.Encoding
let chunkSize : Int
var fileHandle : FileHandle!
let buffer : NSMutableData!
var atEof : Bool = false
var characterPointer: UnsafeMutablePointer<Character>
var startPointer: UnsafeMutablePointer<Character>
var stored_cnt: Int = 0;
var stored_idx: Int = 0;
init?(path: String, encoding: String.Encoding = String.Encoding.utf8, chunkSize : Int = 1024) {
self.chunkSize = chunkSize
self.encoding = encoding
characterPointer = UnsafeMutablePointer<Character>.allocate(capacity: chunkSize)
startPointer = characterPointer
if let fileHandle = FileHandle(forReadingAtPath: path),
let buffer = NSMutableData(capacity: chunkSize){
self.fileHandle = fileHandle
self.buffer = buffer
} else {
self.fileHandle = nil
self.buffer = nil
return nil
}
}
deinit {
self.close()
}
func nextChar() -> Character? {
if atEof {
return nil
}
if stored_cnt > (stored_idx + 1) {
stored_idx += 1
let char = characterPointer.pointee
characterPointer = characterPointer.successor()
return char
}
let tmpData = fileHandle.readData(ofLength: (chunkSize))
if tmpData.count == 0 {
atEof = true
return nil
}
if let s = NSString(data: tmpData, encoding: encoding.rawValue) as String! {
stored_idx = 0
let characters = s.characters
stored_cnt = characters.count
characterPointer = startPointer
characterPointer.initialize(from: characters)
let char = characterPointer.pointee
characterPointer = characterPointer.successor()
return char
}
return nil;
}
/// Close the underlying file. No reading must be done after calling this method.
func close() -> Void {
fileHandle?.closeFile()
fileHandle = nil
}
}
please let me know.
I test the class with this main.swfit:
import Foundation
if CommandLine.arguments.count < 2 {
print("Too less arguments.")
exit(0)
}
let file = CommandLine.arguments[1]
if let aCharReader = CharReader(path: file) {
defer {
aCharReader.close()
}
while let char = aCharReader.nextChar() {
continue
}
}
The Project is on GitHub: https://github.com/petershaw/charsinfile
Thanks a lot,
ps
i updated the repository with both versions in it: https://github.com/petershaw/charsinfile
With help from Martin I fix the mistake in Rob's code.
I tested a bunch of different files and both versions worked fine. Rob Napier's code is more efficient! Thanks a lot, Rob.
Thanks all of you both to help me figuring out the fastest solution. It's gerate to have a so wonderful and polite community for swift and cocoa related stuff here an so.
Have a great week!
ps

Swift2, Call swift function in CFSocketCallBack - EXEC_BAD_ACCESS

I'm trying to write a socket server app for Mac OSX with Xcode:7.2.1 in Swift2.1.1. referring to CocoaEcho sample code.
But I cannot call a swift function in the socketCallBack function.
My code is here. I'm passing the self based on the answer at Swift 2 - UnsafeMutablePointer to object. And I think the part of the code is working ok.
class myServer: NSObject {
// sockets
private var socketipv4: CFSocket!
private var socketipv6: CFSocket!
// Connections
var connections = Set<SPFConnection>()
func start(address: String) -> Bool {
var sockCtxt = CFSocketContext(version: 0, info: nil, retain: nil, release: nil, copyDescription: nil)
sockCtxt.info = UnsafeMutablePointer(unsafeAddressOf(self))
// create socket with CFSocketCreate
socketipv4 = CFSocketCreate(
kCFAllocatorDefault,
PF_INET,
SOCK_STREAM,
IPPROTO_TCP,
kCFSocketAutomaticallyReenableAcceptCallBack,
socketCallBack,
&sockCtxt)
// ipv4
var sin = sockaddr_in() // = initStruct()
let server_addr_size = socklen_t(INET_ADDRSTRLEN)
sin.sin_len = UInt8(server_addr_size)
sin.sin_family = sa_family_t(AF_INET)
sin.sin_port = UInt16(9999).bigEndian
sin.sin_addr.s_addr = inet_addr(address)
let sinData = NSData(bytes: &sin, length: sizeof(sockaddr_in))
let ptr = UnsafePointer<UInt8>(sinData.bytes)
let sincfd = CFDataCreate(kCFAllocatorDefault, ptr, sizeof(sockaddr_in))
let ipv4SocketError: CFSocketError = CFSocketSetAddress(socketipv4, sincfd)
switch ipv4SocketError {
case .Success:
print("ipv4 Success")
default:
print("ipv4 error = \(ipv4SocketError.rawValue)")
return false
}
let socketSource = CFSocketCreateRunLoopSource(kCFAllocatorDefault, socketipv4, 0)
CFRunLoopAddSource(CFRunLoopGetCurrent(), socketSource, kCFRunLoopDefaultMode)
return true
}
// CFSocket call back
var socketCallBack : #convention(c)(CFSocket!, CFSocketCallBackType, CFData!, UnsafePointer<Void>, UnsafeMutablePointer<Void>) -> Void = {
(socketRef, callbackType, address, data, info) in
print("acceptConnection callback-ed") // \(socketRef), \(callbackType), \(address), \(data),\(info)")
var tempData: CFSocketNativeHandle = 0
var anNSData:NSData = NSData(bytes: data, length: sizeofValue(data))
anNSData.getBytes(&tempData, length: sizeof(CFSocketNativeHandle))
var tempAry = [UnsafeMutablePointer<Void>]()
tempAry.append(info)
if callbackType == CFSocketCallBackType.AcceptCallBack {
let server = unsafeBitCast(info, myServer.self)
// **** EXEC_BAD_ACCESS, code=2 ***** //
server.acceptConnection(tempData)
} else {
print("callbacktype = \(callbackType.rawValue)")
}
}
func acceptConnection(data: CFSocketNativeHandle) {
print("acceptConnection called")
var readStream: Unmanaged<CFReadStream>?
var writeStream: Unmanaged<CFWriteStream>?
CFStreamCreatePairWithSocket(kCFAllocatorDefault, data, &readStream, &writeStream)
if readStream != nil && writeStream != nil {
CFReadStreamSetProperty(readStream!.takeUnretainedValue(), kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue)
CFWriteStreamSetProperty(writeStream!.takeUnretainedValue(), kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue)
let connection = SPFConnection()
connection.inputStream = readStream!.takeRetainedValue()
connection.outputStream = writeStream!.takeRetainedValue()
if connection.open() {
connections.insert(connection)
}
}
}
}
I'm getting EXEC_BAD_ACCESS code=2 at the code server.acceptConnection(tempData).
Debugger shows same pointer for both info and server, which should mean info(self) is properly assigned to server.
But EXEC_BAD_ACCESS seems to mean self is no longer available.
I'm struggling to find a solution. If anyone could give me any advise,
it'd be very much appreciated.
Thanks in advance for your help.
I'll admit I don't know much about using these lower level C conventions, but since the callback function is part of your class, can't just say self.acceptConnection(tempData)?

Swift pointer problems with MACH_TASK_BASIC_INFO

I am trying to convert an ObjC stackoverflow answer to Swift and failing. It looks like I am passing a UnsafeMutablePointer<mach_msg_type_number_t> when I should be passing an inout mach_msg_type_number_t and I can't seem to work out my problem. From what I understand of the Swift pointer documentation (not much) these should be interchangeable..?
Further info below.
Here's the Objective C:
struct task_basic_info info;
mach_msg_type_number_t size = sizeof(info);
kern_return_t kerr = task_info(mach_task_self(), TASK_BASIC_INFO, (task_info_t)&info, &size);
and here's as far as I got in Swift (many lines for easier type checking)
let name: task_name_t = mach_task_self_
let flavor: task_flavor_t = task_flavor_t(MACH_TASK_BASIC_INFO)
var info: mach_task_basic_info
var size: mach_msg_type_number_t = UnsignedFixed(sizeof(mach_task_basic_info_t))
let kerr = task_info(name, flavor, info as task_info_t, &size)
The task_info signature is:
func task_info(target_task: task_name_t, flavor: task_flavor_t, task_info_out: task_info_t, task_info_outCnt: UnsafeMutablePointer<mach_msg_type_number_t>) -> kern_return_t
and the error on the last line is:
Cannot convert the expression's type '(#!lvalue task_name_t, task_flavor_t, task_info_t, inout mach_msg_type_number_t)' to type 'kern_return_t'
Took me a bit to update Airspeed Velocity's answer to the latest swift syntax (Swift 3, beta 6), but here is what I got:
func report_memory() {
var info = mach_task_basic_info()
let MACH_TASK_BASIC_INFO_COUNT = MemoryLayout<mach_task_basic_info>.stride/MemoryLayout<natural_t>.stride
var count = mach_msg_type_number_t(MACH_TASK_BASIC_INFO_COUNT)
let kerr: kern_return_t = withUnsafeMutablePointer(to: &info) {
$0.withMemoryRebound(to: integer_t.self, capacity: MACH_TASK_BASIC_INFO_COUNT) {
task_info(mach_task_self_,
task_flavor_t(MACH_TASK_BASIC_INFO),
$0,
&count)
}
}
if kerr == KERN_SUCCESS {
print("Memory in use (in bytes): \(info.resident_size)")
}
else {
print("Error with task_info(): " +
(String(cString: mach_error_string(kerr), encoding: String.Encoding.ascii) ?? "unknown error"))
}
}
Hope that's helpful.
When interacting with C functions, you can't rely on the compiler's error messages - break it down parameter by parameter, command-clicking until you know what you're working with. To start with, the types you're running into are:
task_name_t: UInt32
task_flavor_t: UInt32
task_info_t: UnsafeMutablePointer<Int32>
UnsafeMutablePointer<mach_msg_type_number_t>: UnsafeMutablePointer<UInt32>
kern_return_t - Int32
There's one tricky Swift bit along with a bug in your code standing in your way here. First, the task_info_out parameter needs to be a UnsafeMutablePointer<UInt32>, but needs to actually point to an instance of mach_task_basic_info. We can get around this by creating a UnsafeMutablePointer<mach_task_basic_info> and wrapping it in another UnsafeMutablePointer at call time - the compiler will use type inference to know we want that wrapping pointer to be sub-typed as UInt32.
Second, you're calling sizeof(mach_task_basic_info_t) (the pointer to mach_task_basic_info) when you should be calling sizeinfo(mach_task_basic_info), so your byte count ends up too low to hold the data structure.
On further research, this got a little more complicated. The original code for this was incorrect, in that size should be initialized to the constant MACH_TASK_BASIC_INFO_COUNT. Unfortunately, that's a macro, not a simple constant:
#define MACH_TASK_BASIC_INFO_COUNT (sizeof(mach_task_basic_info_data_t) / sizeof(natural_t))
Swift doesn't import those, so we'll need to redefine it ourselves. Here's working code for all this:
// constant
let MACH_TASK_BASIC_INFO_COUNT = (sizeof(mach_task_basic_info_data_t) / sizeof(natural_t))
// prepare parameters
let name = mach_task_self_
let flavor = task_flavor_t(MACH_TASK_BASIC_INFO)
var size = mach_msg_type_number_t(MACH_TASK_BASIC_INFO_COUNT)
// allocate pointer to mach_task_basic_info
var infoPointer = UnsafeMutablePointer<mach_task_basic_info>.alloc(1)
// call task_info - note extra UnsafeMutablePointer(...) call
let kerr = task_info(name, flavor, UnsafeMutablePointer(infoPointer), &size)
// get mach_task_basic_info struct out of pointer
let info = infoPointer.move()
// deallocate pointer
infoPointer.dealloc(1)
// check return value for success / failure
if kerr == KERN_SUCCESS {
println("Memory in use (in bytes): \(info.resident_size)")
} else {
let errorString = String(CString: mach_error_string(kerr), encoding: NSASCIIStringEncoding)
println(errorString ?? "Error: couldn't parse error string")
}
For a quick copy and paste solution in Swift 5, use
func reportMemory() {
var taskInfo = task_vm_info_data_t()
var count = mach_msg_type_number_t(MemoryLayout<task_vm_info>.size) / 4
let result: kern_return_t = withUnsafeMutablePointer(to: &taskInfo) {
$0.withMemoryRebound(to: integer_t.self, capacity: 1) {
task_info(mach_task_self_, task_flavor_t(TASK_VM_INFO), $0, &count)
}
}
let usedMb = Float(taskInfo.phys_footprint) / 1048576.0
let totalMb = Float(ProcessInfo.processInfo.physicalMemory) / 1048576.0
result != KERN_SUCCESS ? print("Memory used: ? of \(totalMb)") : print("Memory used: \(usedMb) of \(totalMb)")
}
Nate’s answer is excellent but there’s a tweak you can make to simplify it.
First, rather than allocating/deallocating the task_basic_info pointer, you can create the struct on the stack, then use withUnsafeMutablePointer to get a pointer directly to it which you can pass in.
func report_memory() {
var info = mach_task_basic_info()
var count = mach_msg_type_number_t(sizeofValue(info))/4
let kerr: kern_return_t = withUnsafeMutablePointer(&info) {
task_info(mach_task_self_,
task_flavor_t(MACH_TASK_BASIC_INFO),
task_info_t($0),
&count)
}
if kerr == KERN_SUCCESS {
println("Memory in use (in bytes): \(info.resident_size)")
}
else {
println("Error with task_info(): " +
(String.fromCString(mach_error_string(kerr)) ?? "unknown error"))
}
}
Airspeed Velocity's answer in Swift 3...
func GetMemory()
{
var info = mach_task_basic_info()
var count = mach_msg_type_number_t(MemoryLayout.size(ofValue: info))/4
let kerr: kern_return_t = withUnsafeMutablePointer(to: &info)
{
task_info(mach_task_self_,
task_flavor_t(MACH_TASK_BASIC_INFO),
$0.withMemoryRebound(to: Int32.self, capacity: 1) { zeroPtr in
task_info_t(zeroPtr)
},
&count)
}
if kerr == KERN_SUCCESS {
print("Memory in use (in bytes): \(info.resident_size)")
}
else {
print("Error with task_info(): " +
(String.init(validatingUTF8: mach_error_string(kerr)) ?? "unknown error"))
}
}
Swift 5 + Combine, Continuous memory Monitoring
Show exact memory in MB like XCODE
import Foundation
import Combine
enum MemoryMonitorState {
case started
case paused
}
class MemoryUsageCustom {
private var displayLink: CADisplayLink!
var state = MemoryMonitorState.paused
let subject = PassthroughSubject<String, Never>()
private static var sharedInstance: MemoryUsageCustom!
public class func shared() -> MemoryUsageCustom {
if self.sharedInstance == nil {
self.sharedInstance = MemoryUsageCustom()
}
return self.sharedInstance
}
private init() {
self.configureDisplayLink()
}
func startMemoryMonitor() {
if self.state == .started {
return
}
self.state = .started
self.start()
}
func stopMemoryMonitor() {
self.state = .paused
self.pause()
}
//--------------------------------------------------------------------------------
//MARK:- Display Link
//--------------------------------------------------------------------------------
func configureDisplayLink() {
self.displayLink = CADisplayLink(target: self, selector: #selector(displayLinkAction(displayLink:)))
self.displayLink.isPaused = true
self.displayLink?.add(to: .current, forMode: .common)
}
private func start() {
self.displayLink?.isPaused = false
}
/// Pauses performance monitoring.
private func pause() {
self.displayLink?.isPaused = true
}
#objc func displayLinkAction(displayLink: CADisplayLink) {
let memoryUsage = self.memoryUsage()
let bytesInMegabyte = 1024.0 * 1024.0
let usedMemory = Double(memoryUsage.used) / bytesInMegabyte
let totalMemory = Double(memoryUsage.total) / bytesInMegabyte
let memory = String(format: "%.1f of %.0f MB used", usedMemory, totalMemory)
// self.memoryString = memory
subject.send(memory)
}
func memoryUsage() -> (used: UInt64, total: UInt64) {
var taskInfo = task_vm_info_data_t()
var count = mach_msg_type_number_t(MemoryLayout<task_vm_info>.size) / 4
let result: kern_return_t = withUnsafeMutablePointer(to: &taskInfo) {
$0.withMemoryRebound(to: integer_t.self, capacity: 1) {
task_info(mach_task_self_, task_flavor_t(TASK_VM_INFO), $0, &count)
}
}
var used: UInt64 = 0
if result == KERN_SUCCESS {
used = UInt64(taskInfo.phys_footprint)
}
let total = ProcessInfo.processInfo.physicalMemory
return (used, total)
}
}
How To use
//Start Monitoring
MemoryUsageCustom.shared().startMemoryMonitor()
var storage = Set<AnyCancellable>()
MemoryUsageCustom.shared().subject.sink {[weak self] (string) in
print(string)
}.store(in: &storage)
For Linux:
import Foundation
#available(macOS 10.13, *)
public func shell(_ args: String...) throws -> String? {
let task = Process()
task.launchPath = "/usr/bin/env"
task.arguments = args
let pipe = Pipe()
task.standardOutput = pipe
task.standardError = pipe
try task.run()
let data = pipe.fileHandleForReading.readDataToEndOfFile()
if let output = String(data: data, encoding: String.Encoding.utf8) {
if output.count > 0 {
//remove newline character.
let lastIndex = output.index(before: output.endIndex)
return String(output[output.startIndex ..< lastIndex])
}
task.waitUntilExit()
return output
} else {
return nil
}
}
#available(macOS 10.13, *)
public func shellWithPipes(_ args: String...) throws -> String? {
var task: Process!
var prevPipe: Pipe? = nil
guard args.count > 0 else {
return nil
}
for i in 0..<args.count {
task = Process()
task.launchPath = "/usr/bin/env"
let taskArgs = args[i].components(separatedBy: " ")
var refinedArgs = [String]()
var refinedArg = ""
for arg in taskArgs {
if !refinedArg.isEmpty {
refinedArg += " " + arg
if arg.suffix(1) == "'" {
refinedArgs.append(refinedArg.replacingOccurrences(of: "\'", with: ""))
refinedArg = ""
}
} else {
if arg.prefix(1) == "'" {
refinedArg = arg
} else {
refinedArgs.append(arg)
}
}
}
task.arguments = refinedArgs
let pipe = Pipe()
if let prevPipe = prevPipe {
task.standardInput = prevPipe
}
task.standardOutput = pipe
task.standardError = pipe
try task.run()
prevPipe = pipe
}
if let data = prevPipe?.fileHandleForReading.readDataToEndOfFile(),
let output = String(data: data, encoding: String.Encoding.utf8) {
if output.count > 0 {
//remove newline character.
let lastIndex = output.index(before: output.endIndex)
return String(output[output.startIndex ..< lastIndex])
}
task.waitUntilExit()
return output
}
return nil
}
#if os(Linux)
public func reportMemory() {
do {
if let usage = try shellWithPipes("free -m", "grep Mem", "awk '{print $3 \"MB of \" $2 \"MB\"}'") {
NSLog("Memory used: \(usage)")
}
} catch {
NSLog("reportMemory error: \(error)")
}
}
public func availableMemory() -> Int {
do {
if let avaiable = try shellWithPipes("free -m", "grep Mem", "awk '{print $7}'") {
return Int(avaiable) ?? -1
}
} catch {
NSLog("availableMemory error: \(error)")
}
return -1
}
public func freeMemory() -> Int {
do {
if let result = try shellWithPipes("free -m", "grep Mem", "awk '{print $4}'") {
return Int(result) ?? -1
}
} catch {
NSLog("freeMemory error: \(error)")
}
return -1
}
#endif

iPhone Data Usage Tracking/Monitoring

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 compare the details shared in this post with other posts before marking this as DUPLICATE, and not just by the subject.
- (NSArray *)getDataCountersForType:(int)type {
BOOL success;
struct ifaddrs *addrs = nil;
const struct ifaddrs *cursor = nil;
const struct sockaddr_dl *dlAddr = nil;
const struct if_data *networkStatisc = nil;
int dataSent = 0;
int dataReceived = 0;
success = getifaddrs(&addrs) == 0;
if (success) {
cursor = addrs;
while (cursor != NULL) {
if (cursor->ifa_addr->sa_family == AF_LINK) {
dlAddr = (const struct sockaddr_dl *) cursor->ifa_addr;
networkStatisc = (const struct if_data *) cursor->ifa_data;
if (type == WiFi) {
dataSent += networkStatisc->ifi_opackets;
dataReceived += networkStatisc->ifi_ipackets;
}
else if (type == WWAN) {
dataSent += networkStatisc->ifi_obytes;
dataReceived += networkStatisc->ifi_ibytes;
}
}
cursor = cursor->ifa_next;
}
freeifaddrs(addrs);
}
return [NSArray arrayWithObjects:[NSNumber numberWithInt:dataSent], [NSNumber numberWithInt:dataReceived], nil];
}
This code collects information of internet usage of an iPhone device (and not my application alone).
Now, if I use internet through WiFi or through 3G, I get the the data (bytes) only in ifi_obytes (sent) and ifi_ibytes (received) but I think I should get WiFi usage in ifi_opackets and ifi_ipackets.
Also wanted to add that if I'm connected to a WiFi network, but am not using internet, I still get value added to ifi_obytes and ifi_ibytes.
May be I'm wrong in the implementation or understanding. Need someone to help me out.
Edit: Instead of AF_LINK I tried AF_INET (sockaddr_in instead of sockaddr_dl). This crashes the application.
The thing is that pdp_ip0 is one of interfaces, all pdpXXX are WWAN interfaces dedicated to different functions, voicemail, general networking interface.
I read in Apple forum that :
The OS does not keep network statistics on a process-by-process basis. As such, there's no exact solution to this problem. You can, however, get network statistics for each network interface.
In general en0 is your Wi-Fi interface and pdp_ip0 is your WWAN interface.
There is no good way to get information wifi/cellular network data since, particular date-time!
Data statistic (ifa_data->ifi_obytes and ifa_data->ifi_ibytes) are stored from previous device reboot.
I don't know why, but ifi_opackets and ifi_ipackets are shown just for lo0 (I think its main interface ).
Yes. Then device is connected via WiFi and doesn't use internet if_iobytes values still come because this method provides network bytes exchanges and not just internet.
#include <net/if.h>
#include <ifaddrs.h>
static NSString *const DataCounterKeyWWANSent = #"WWANSent";
static NSString *const DataCounterKeyWWANReceived = #"WWANReceived";
static NSString *const DataCounterKeyWiFiSent = #"WiFiSent";
static NSString *const DataCounterKeyWiFiReceived = #"WiFiReceived";
NSDictionary *DataCounters()
{
struct ifaddrs *addrs;
const struct ifaddrs *cursor;
u_int32_t WiFiSent = 0;
u_int32_t WiFiReceived = 0;
u_int32_t WWANSent = 0;
u_int32_t WWANReceived = 0;
if (getifaddrs(&addrs) == 0)
{
cursor = addrs;
while (cursor != NULL)
{
if (cursor->ifa_addr->sa_family == AF_LINK)
{
#ifdef DEBUG
const struct if_data *ifa_data = (struct if_data *)cursor->ifa_data;
if (ifa_data != NULL)
{
NSLog(#"Interface name %s: sent %tu received %tu",cursor->ifa_name,ifa_data->ifi_obytes,ifa_data->ifi_ibytes);
}
#endif
// name of interfaces:
// en0 is WiFi
// pdp_ip0 is WWAN
NSString *name = #(cursor->ifa_name);
if ([name hasPrefix:#"en"])
{
const struct if_data *ifa_data = (struct if_data *)cursor->ifa_data;
if (ifa_data != NULL)
{
WiFiSent += ifa_data->ifi_obytes;
WiFiReceived += ifa_data->ifi_ibytes;
}
}
if ([name hasPrefix:#"pdp_ip"])
{
const struct if_data *ifa_data = (struct if_data *)cursor->ifa_data;
if (ifa_data != NULL)
{
WWANSent += ifa_data->ifi_obytes;
WWANReceived += ifa_data->ifi_ibytes;
}
}
}
cursor = cursor->ifa_next;
}
freeifaddrs(addrs);
}
return #{DataCounterKeyWiFiSent : #(WiFiSent),
DataCounterKeyWiFiReceived : #(WiFiReceived),
DataCounterKeyWWANSent : #(WWANSent),
DataCounterKeyWWANReceived : #(WWANReceived)};
}
Improved copy/paste support !
It's important to understand that these counters are provided since the device's last boot.
So, to make effective use of them, you should accompany every sample with the device's uptime (you can use mach_absolute_time() - see this for more information)
Once you have counters samples + uptime you can have better heuristics as to data use...
To add to the accepted answer, its important to realize that the amount of data displayed by the interface overflows and restarts at 0 after every 4 GB, especially if you are using this code to calculate the difference between two readings. This is because ifi_obytes and ifi_ibytes are uint_32 and their max value is 4294967295.
Also, I recommend using unsigned ints for the variables containing the data sent and received. Regular ints have half the max value of an unsigned integer, so when adding ifi_obytes, it may cause an overflow.
unsigned int sent = 0;
sent += networkStatisc->ifi_obytes;
Swift version of the accepted answer. I also break the code into smaller units.
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 interfaceAddresses: UnsafeMutablePointer<ifaddrs> = nil
var dataUsageInfo = DataUsageInfo()
guard getifaddrs(&interfaceAddresses) == 0 else { return dataUsageInfo }
var pointer = interfaceAddresses
while pointer != nil {
guard let info = getDataUsageInfo(from: pointer) else {
pointer = pointer.memory.ifa_next
continue
}
dataUsageInfo.updateInfoByAdding(info)
pointer = pointer.memory.ifa_next
}
freeifaddrs(interfaceAddresses)
return dataUsageInfo
}
private class func getDataUsageInfo(from infoPointer: UnsafeMutablePointer<ifaddrs>) -> DataUsageInfo? {
let pointer = infoPointer
let name: String! = String.fromCString(infoPointer.memory.ifa_name)
let addr = pointer.memory.ifa_addr.memory
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> = nil
var dataUsageInfo = DataUsageInfo()
if name.hasPrefix(wifiInterfacePrefix) {
networkData = unsafeBitCast(pointer.memory.ifa_data, UnsafeMutablePointer<if_data>.self)
dataUsageInfo.wifiSent += networkData.memory.ifi_obytes
dataUsageInfo.wifiReceived += networkData.memory.ifi_ibytes
} else if name.hasPrefix(wwanInterfacePrefix) {
networkData = unsafeBitCast(pointer.memory.ifa_data, UnsafeMutablePointer<if_data>.self)
dataUsageInfo.wirelessWanDataSent += networkData.memory.ifi_obytes
dataUsageInfo.wirelessWanDataReceived += networkData.memory.ifi_ibytes
}
return dataUsageInfo
}
}
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<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 += data.pointee.ifi_obytes
dataUsageInfo.wifiReceived += 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 += data.pointee.ifi_obytes
dataUsageInfo.wirelessWanDataReceived += data.pointee.ifi_ibytes
}
}
return dataUsageInfo
}
}
A new version about based on previous versions, but adapted for Swift4 and Xcode 9
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 interfaceAddresses: UnsafeMutablePointer<ifaddrs>? = nil
var dataUsageInfo = DataUsageInfo()
guard getifaddrs(&interfaceAddresses) == 0 else { return dataUsageInfo }
var pointer = interfaceAddresses
while pointer != nil {
guard let info = getDataUsageInfo(from: pointer!) else {
pointer = pointer!.pointee.ifa_next
continue
}
dataUsageInfo.updateInfoByAdding(info: info)
pointer = pointer!.pointee.ifa_next
}
freeifaddrs(interfaceAddresses)
return dataUsageInfo
}
private class func getDataUsageInfo(from infoPointer: UnsafeMutablePointer<ifaddrs>) -> DataUsageInfo? {
let pointer = infoPointer
let name: String! = String(cString: infoPointer.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>? = nil
var dataUsageInfo = DataUsageInfo()
if name.hasPrefix(wifiInterfacePrefix) {
networkData = unsafeBitCast(pointer.pointee.ifa_data, to: UnsafeMutablePointer<if_data>.self)
dataUsageInfo.wifiSent += networkData?.pointee.ifi_obytes ?? 0
dataUsageInfo.wifiReceived += networkData?.pointee.ifi_ibytes ?? 0
} else if name.hasPrefix(wwanInterfacePrefix) {
networkData = unsafeBitCast(pointer.pointee.ifa_data, to: UnsafeMutablePointer<if_data>.self)
dataUsageInfo.wirelessWanDataSent += networkData?.pointee.ifi_obytes ?? 0
dataUsageInfo.wirelessWanDataReceived += networkData?.pointee.ifi_ibytes ?? 0
}
return dataUsageInfo
}
}
Sorry for same answer again.
but I found that UInt32 is not enough, so it crashes when it became too big.
I just changed UInt32 to UInt64 and it works fine.
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
}
}
class DataUsage {
private static let wwanInterfacePrefix = "pdp_ip"
private static let wifiInterfacePrefix = "en"
class func getDataUsage() -> DataUsageInfo {
var interfaceAddresses: UnsafeMutablePointer<ifaddrs>? = nil
var dataUsageInfo = DataUsageInfo()
guard getifaddrs(&interfaceAddresses) == 0 else { return dataUsageInfo }
var pointer = interfaceAddresses
while pointer != nil {
guard let info = getDataUsageInfo(from: pointer!) else {
pointer = pointer!.pointee.ifa_next
continue
}
dataUsageInfo.updateInfoByAdding(info: info)
pointer = pointer!.pointee.ifa_next
}
freeifaddrs(interfaceAddresses)
return dataUsageInfo
}
private class func getDataUsageInfo(from infoPointer: UnsafeMutablePointer<ifaddrs>) -> DataUsageInfo? {
let pointer = infoPointer
let name: String! = String(cString: infoPointer.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>? = nil
var dataUsageInfo = DataUsageInfo()
if name.hasPrefix(wifiInterfacePrefix) {
networkData = unsafeBitCast(pointer.pointee.ifa_data, to: UnsafeMutablePointer<if_data>.self)
dataUsageInfo.wifiSent += UInt64(networkData?.pointee.ifi_obytes ?? 0)
dataUsageInfo.wifiReceived += UInt64(networkData?.pointee.ifi_ibytes ?? 0)
} else if name.hasPrefix(wwanInterfacePrefix) {
networkData = unsafeBitCast(pointer.pointee.ifa_data, to: UnsafeMutablePointer<if_data>.self)
dataUsageInfo.wirelessWanDataSent += UInt64(networkData?.pointee.ifi_obytes ?? 0)
dataUsageInfo.wirelessWanDataReceived += UInt64(networkData?.pointee.ifi_ibytes ?? 0)
}
return dataUsageInfo
}
}