import Foundation import SystemConfiguration var reach:SCNetworkReachability! reach = SCNetworkReachabilityCreateWithName(kCFAllocatorDefault, "apple.com").takeRetainedValue() typealias ChangeBlock = (UInt32) -> () let block:ChangeBlock = {status in // swift compleation block } let blockPtr = UnsafeMutablePointer.alloc(1) blockPtr.initialize(block) let blockVoidPtr = UnsafeMutablePointer(blockPtr) // Create callback let callback:(SCNetworkReachability!, SCNetworkReachabilityFlags, UnsafeMutablePointer) -> () = { (target, flags, info) in let ptr = UnsafeMutablePointer(info) let block:ChangeBlock = ptr.memory block(flags) } // Create UnsafeMutablePointer and initialise with callback let callbackPtr = UnsafeMutablePointer<(SCNetworkReachability!, SCNetworkReachabilityFlags, UnsafeMutablePointer) -> Void>.alloc(1) callbackPtr.initialize(callback) // convert UnsafeMutablePointer to COpaquePointer let cop = COpaquePointer(callbackPtr) // convert COppaquePointer to CFunctionPointer let cfptr = CFunctionPointer<(SCNetworkReachability!, SCNetworkReachabilityFlags, UnsafeMutablePointer) -> Void>(cop) let context = SCNetworkReachabilityContext(version: 0, info: blockVoidPtr, retain: nil, release: nil, copyDescription: nil) let contextPtr = UnsafeMutablePointer.alloc(1) contextPtr.initialize(context) SCNetworkReachabilitySetCallback(reach, cfptr, contextPtr) let que = dispatch_queue_create("me.swiftti.reachability", nil) SCNetworkReachabilitySetDispatchQueue(reach, que) // on deinit() do contextPtr.destroy() contextPtr.dealloc(1) callbackPtr.destroy() callbackPtr.dealloc(1) blockVoidPtr.destroy() blockVoidPtr.dealloc(1)