【问题标题】:Get the Job Status of UIPrintInteractionController in Swift在 Swift 中获取 UIPrintInteractionController 的作业状态
【发布时间】:2015-12-28 14:38:02
【问题描述】:

我有一个问题:我想知道打印作业的状态。我正在打印一个临时存储在云驱动器中的 PDF 文件。我想在打印作业完成后删除该文件。我怎样才能迅速意识到这一点? 这是我的代码的 sn-p:

 // 1
    let printController = UIPrintInteractionController.sharedPrintController()
    // 2
    let printInfo = UIPrintInfo(dictionary:nil)
    printInfo.outputType = UIPrintInfoOutputType.General
    printController.showsNumberOfCopies = false
    printController.showsPageRange = false
    printInfo.jobName = "PDF ID: " + pdfObjectID
    printController.printInfo = printInfo

    // 3
    //let formatter = UIMarkupTextPrintFormatter(markupText: "Test")
    let formatter = pdf.viewPrintFormatter()
    formatter.contentInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
    printController.printFormatter = formatter

    // show PrintController
    printController.presentAnimated(true, completionHandler: nil)

你有什么建议吗? 谢谢:)

【问题讨论】:

  • 你不能为此目的使用完成处理程序吗?
  • 是的,我想是的,但我不知道该怎么做。参考这个页面UIKit Framework Reference我必须使用方法UIPrintInteractionCompletionHandler。你能给我举个例子吗?

标签: ios swift ios9 airprint


【解决方案1】:

我自己没有测试过,但是根据文档, 您可以将UIPrintInteractionCompletionHandler 作为completionHandler: 参数传递:

当打印作业结束时,您可以重置为打印设置的任何状态并执行相关的内务管理任务。

一个简单的例子:

    printController.presentAnimated(true) { (controller, success, error) -> Void in
        if success {
            // Printed successfully
            // Remove file here ...
        } else {
            // Printing failed, report error ...
        }
    }

【讨论】:

  • Mh 这段代码看起来不错,但它不起作用。我添加了一个窗口来确认打印是否正常,但如果我能自动解决这个问题就太好了
【解决方案2】:

为了补充 Martin R 的答案,您可以通过声明闭包来添加完成处理程序。如果您想根据设备以不同方式呈现打印控制器,这将非常有用:

    let printCompletionHandler: UIPrintInteractionCompletionHandler = { (controller, success, error) -> Void in
        if success {
            // Printed successfully
            // Remove file here ...
        } else {
            // Printing failed, report error ...
        }
    }

    if UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.Pad {
        printController.presentFromRect(self.printButton.frame, inView: self.view, animated: true, completionHandler: printCompletionHandler)
    } else {
        printController.presentAnimated(true, completionHandler: printCompletionHandler)
    }

这样您就可以对两个调用使用相同的完成处理程序:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多