This file contains hidden or 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
| var manager: EAAccessoryManager! | |
| var isConnected: Bool = false | |
| private var printerConnection: MfiBtPrinterConnection? | |
| private var serialNumber: String? | |
| private var disconnectNotificationObserver: NSObjectProtocol? | |
| private var connectedNotificationObserver: NSObjectProtocol? | |
| static let sharedInstance = PrintManager() |
This file contains hidden or 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
| class ViewController: UIViewController { | |
| @IBOutlet var printerConnectionStatus: UILabel! | |
| var printManager = PrintManager.sharedInstance | |
| override func viewDidLoad() { | |
| super.viewDidLoad() | |
| printManager.connectionDelegate = self | |
| if printManager.isConnected { | |
| printerConnectionStatus.text = “Connected” | |
| } else { | |
| printerConnectionStatus.text = “Not Connected” |
This file contains hidden or 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
| private func printerTextField(font:Int, size: Int, x:Int, y: Int, content: String) -> String { | |
| return “TEXT \(font) \(size) \(x) \(y) \(content)” | |
| } | |
| private func printMultiLineTextField(linesHeight: Int, font:Int, size: Int, x:Int, y: Int, content: String) -> String { | |
| return “ML \(linesHeight)\nTEXT \(font) \(size) \(x) \(y) \n\(content)\nENDML\nENDML” | |
| } | |
| private func printerBarCodeFormat(width: Int, ratio: Int, height: Int, x: Int, y:Int, content: String) -> String { | |
| return “BARCODE 128 \(width) \(ratio) \(height) \(x) \(y) \(content)” | |
| } |
This file contains hidden or 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
| func findConnectedPrinter(completion: (Bool) -> Void) { | |
| let connectedDevices = manager.connectedAccessories | |
| for device in connectedDevices { | |
| if device.protocolStrings.contains(“com.zebra.rawport”) { | |
| serialNumber = device.serialNumber | |
| connectToPrinter(completion: { completed in | |
| completion(completed) | |
| }) | |
| } | |
| } |
This file contains hidden or 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
| deinit { | |
| if let disconnectNotificationObserver = disconnectNotificationObserver { | |
| NotificationCenter.default.removeObserver(disconnectNotificationObserver) | |
| } | |
| if let connectedNotificationObserver = connectedNotificationObserver { | |
| NotificationCenter.default.removeObserver(connectedNotificationObserver) | |
| } | |
| } |
This file contains hidden or 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
| private override init() { | |
| super.init() | |
| manager = EAAccessoryManager.shared() | |
| findConnectedPrinter { [weak self] bool in | |
| if let strongSelf = self { | |
| strongSelf.isConnected = bool | |
| } | |
| } | |
| //Notifications | |
| disconnectNotificationObserver = NotificationCenter.default.addObserver(forName: Notification.Name.EAAccessoryDidDisconnect, object: nil, queue: nil, using: didDisconnect) |
This file contains hidden or 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
| var manager: EAAccessoryManager! | |
| var isConnected: Bool = false | |
| //new content from step1 | |
| var connectionDelegate: EAAccessoryManagerConnectionStatusDelegate? | |
| // end new content | |
| private var printerConnection: MfiBtPrinterConnection? | |
| private var serialNumber: String? | |
| private var disconnectNotificationObserver: NSObjectProtocol? | |
| private var connectedNotificationObserver: NSObjectProtocol? | |
| static let sharedInstance = PrintManager() |
This file contains hidden or 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
| protocol EAAccessoryManagerConnectionStatusDelegate { | |
| func connectionStatusChanged() -> Void | |
| } |
This file contains hidden or 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
| private func didDisconnect(notification: Notification) { | |
| isConnected = false | |
| connectionDelegate?.connectionStatusChanged() | |
| } | |
| private func didConnect(notification: Notification) { | |
| isConnected = true | |
| connectionDelegate?.connectionStatusChanged() | |
| } |
This file contains hidden or 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
| enum CommonPrintingFormat: String { | |
| case start = “! 0 200 200 150 1” | |
| case end = “\nFORM\nPRINT “ | |
| } |