Swift:打印时不带警告框



我在应用程序中使用以下代码进行打印:

    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
  }

它可以成功打印。但是,当该功能被触发时,会出现一个警告框,指示它正在与打印机联系并打印。有什么方法可以避免弹出此警报框吗?我希望在背面完成打印,而不会在屏幕上显示任何干扰用户体验的内容(我想在打印机在背面工作时播放电影(。

谢谢。

从iOS 8开始,有一种方法可以在没有任何演示的情况下进行打印 打印用户界面。而不是在用户每次按 "打印"按钮,您可以为用户提供一种选择打印机的方法 在您的应用程序中的某个位置使用易于使用的 UIPrinterPickerController。 它在其构造函数中接受一个可选的 UIPrinter 实例 预选,使用与上述相同的演示选项, 并且有一个完成处理程序,用于用户何时选择她 打印机:


斯威夫特 3

let printerPicker = UIPrinterPickerController(initiallySelectedPrinter: savedPrinter)
printerPicker.present(animated: true) {
    (printerPicker, userDidSelect, error) in
    if userDidSelect {
        self.savedPrinter = printerPicker.selectedPrinter
    }
}

现在,您可以通过调用printToPrinter(:completionHandler:(来告诉UIPrintInteractionController直接打印使用保存的打印机,而不是使用当前打印机之一...方法。

来源:- http://nshipster.com/uiprintinteractioncontroller/

最新更新