Created
August 7, 2018 07:09
-
-
Save ura14h/6f254f0f972f2cc430521acd6081e482 to your computer and use it in GitHub Desktop.
Using Network.Framework sample for iOS12
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Using Network.Framework sample for iOS12. | |
// a server must run the command: | |
// $ nc -l 3000 | |
import UIKit | |
import Network | |
let connection = NWConnection( | |
host: NWEndpoint.Host("localhost"), | |
port: NWEndpoint.Port(rawValue: 3000)!, | |
using: NWParameters(tls: nil)) | |
let connected = DispatchSemaphore(value: 0) | |
connection.stateUpdateHandler = { state in | |
if (state == .ready) { | |
connected.signal() | |
} | |
} | |
connection.start(queue: DispatchQueue.init(label: "sample")) | |
connected.wait() | |
let sent = DispatchSemaphore(value: 0) | |
connection.send( | |
content: "Hello, World!\n".data(using: .utf8), | |
completion: .contentProcessed( | |
{ error in | |
sent.signal() | |
})) | |
sent.wait() | |
connection.cancel() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment