我正在使用以下代碼在Epson TM-T20
上使用Epson ePOS SDK for iOS
SDK打印某些內容。問題是該應用程序只打印一次。應用程序必須重新啓動才能重新打印。代碼有什麼問題?iOS僅使用Epos2打印機打印一次
printer = Epos2Printer(printerSeries: 2, lang: 1)
printer?.setReceiveEventDelegate(self)
printer?.addText("text")
printer!.connect("TCP:192.168.1.185", timeout:Int(EPOS2_PARAM_DEFAULT))
printer!.beginTransaction()
printer?.sendData(Int(EPOS2_PARAM_DEFAULT))
printer?.endTransaction()
// printer?.disconnect()
printer?.clearCommandBuffer()
printer?.setReceiveEventDelegate(nil)
儘管在文檔中使用,使用printer?.disconnect()
使得應用程序凍結,所以我不得不把它註釋掉。
如果您想查看API文檔,請在the SDK's download內部查看PDF。
更新: 基於答案更新的代碼(應用程序仍然凍結):
func printReceipt() {
var printer: Epos2Printer?
printer = Epos2Printer(printerSeries: 2, lang: 1)
if printer == nil {
print(「Printer not found!! 11")
}
printer?.setReceiveEventDelegate(self)
printer?.addTextFont(2)
printer?.addTextSize(1, height: 1)
printer?.addText(「My Text")
printer?.addFeedUnit(10)
printer?.addCut(0)
var result: Int = Int(EPOS2_SUCCESS.rawValue)
result = Int(printer!.connect("TCP:192.168.1.185", timeout:Int(EPOS2_PARAM_DEFAULT)));
result = Int(printer!.beginTransaction())
printer?.sendData(Int(EPOS2_PARAM_DEFAULT))
DispatchQueue.global(qos: DispatchQoS.QoSClass.default).async {
printer?.clearCommandBuffer()
printer?.setReceiveEventDelegate(nil)
printer?.endTransaction()
printer?.disconnect()
printer = nil;
}
}
無論我叫斷開(),它總是凍結的應用程序。如果我不使用它,打印機似乎被應用程序佔用,以致在我之後沒有其他人可以打印幾分鐘。 – Linus