KevinGong2013 / Printer

Swift ticket printer framework for ESC/POS-compatible thermal printers.
Apache License 2.0
144 stars 72 forks source link

How we can connect wifi printers? #70

Open SHEBINKUNIYIL opened 1 year ago

SHEBINKUNIYIL commented 1 year ago

How we can connect wifi printers?

YanVetrov commented 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!)")
               }
           })))
       }
SHEBINKUNIYIL commented 1 year ago

thank you for the update, but Arabic text and image,s not printing