Skip to content

Instantly share code, notes, and snippets.

@ura14h
Created August 7, 2018 07:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ura14h/6f254f0f972f2cc430521acd6081e482 to your computer and use it in GitHub Desktop.
Save ura14h/6f254f0f972f2cc430521acd6081e482 to your computer and use it in GitHub Desktop.
Using Network.Framework sample for iOS12
// 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