ios - Swift:打印没有警告框

标签 ios swift printing

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

    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:) 来直接打印,而不是使用当前的...方法之一。

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

关于ios - Swift:打印没有警告框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43274089/

相关文章:

ios - iOS 上流畅的全屏 View 转换

objective-c - 我如何将 Microsoft 的 Active Directory 集成到我的 iPhone 应用程序中以在登录时对用户进行身份验证

ios - 为什么使用照片框架的示例应用程序在每次更改后都使用 stopCachingImagesForAllAssets?

ios - swift 用 2 个数组填充 UItableViewCell

python - 带有索引python的打印矩阵

java - 在Java中,我可以在打印时选择开始行/列的位置吗?

ios - 更改Sinch短信验证文本

ios - 如何分离NSMutablearray中存在的元素

xcode - 无法在 Swift 中读取这个闭包

Python 选项卡创建