我在应用程序中使用以下代码进行打印:
init() {
self.printInfo.outputType = UIPrintInfoOutputType.photo
self.printInfo.orientation = UIPrintInfoOrientation.landscape
self.printController.printInfo = self.printInfo
self.printer = UIPrinter(url: URL(string: printIP)!)
// where printIP is a string that give the internal IP of the printer
debugPrint(printIP)
}
func print(image: UIImage) -> Bool {
self.printController.printingItem = image
printController.print(to: printer, completionHandler: {(controller, success, error) -> Void in
if success {
debugPrint("Printing Completed.")
} else {
debugPrint("Printing Failed.")
}
})
return true
}
打印成功。但是,当该功能被触发时,会出现一个警告框,表明它正在联系打印机并进行打印。有什么方法可以避免弹出这个警告框吗?我希望在背面完成打印,而不会在屏幕上显示任何干扰用户体验的内容(我想在打印机在背面工作时播放电影)。
谢谢。
最佳答案
From iOS 8 there is a way to print without any presentation of the printing UI. Instead of presenting the UI each time the user presses a print button, you can provide a way for your users to select a printer somewhere in your app with the easy-to-use UIPrinterPickerController. It accepts an optional UIPrinter instance in its constructor for a pre-selection, uses the same presentation options as explained above, and has a completion handler for when the user has selected her printer:
swift 3
let printerPicker = UIPrinterPickerController(initiallySelectedPrinter: savedPrinter)
printerPicker.present(animated: true) {
(printerPicker, userDidSelect, error) in
if userDidSelect {
self.savedPrinter = printerPicker.selectedPrinter
}
}
现在您可以告诉您的 UIPrintInteractionController 通过使用保存的打印机调用 printToPrinter(:completionHandler:) 来直接打印,而不是使用当前的...方法之一。
关于ios - Swift:打印没有警告框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43274089/