Open SHEBINKUNIYIL opened 1 year ago
Example of direct tcp connection
import Network
import MobileCoreServices
let PORT = NWEndpoint.Port(printerPort)
let ipAddress = NWEndpoint.Host(printerIP)
let queue = DispatchQueue(label: "TCP Client Queue")
let tcp = NWProtocolTCP.Options.init()
tcp.noDelay = true
let params = NWParameters.init(tls: nil, tcp: tcp)
connection = NWConnection(to: NWEndpoint.hostPort(host: ipAddress, port: PORT ?? 9100), using: params)
connection?.stateUpdateHandler = { (newState) in
switch (newState) {
case .ready:
print("Socket State: Ready")
UserDefaults.standard.set(true, forKey: "isConnected")
default:
UserDefaults.standard.set(false, forKey: "isConnected")
break
}
}
connection?.start(queue: queue)
func sendMSG(_ content:Data? = nil) {
print("send data")
connection?.send(content: content, completion: NWConnection.SendCompletion.contentProcessed(({ (NWError) in
if (NWError == nil) {
print("Data was sent to TCP destination ")
} else {
print("ERROR! Error when data (Type: Data) sending. NWError: \n \(NWError!)")
}
})))
}
thank you for the update, but Arabic text and image,s not printing
How we can connect wifi printers?