如何通过UIActivityViewController打印UIWebView?

时间:2017-12-05 18:27:49

标签: ios swift uiwebview uiactivityviewcontroller airprint

我有一个包含UIWebView的视图控制器和一个带有操作/共享按钮的工具栏。这会初始化并显示UIActivityViewController个对象。根据我是使用网络视图的activityItems还是网址的相应URL提供absoluteString参数,会提供不同的操作,但打印选项永远不会显示(也未在“更多”部分中提供)。

我知道如何使用UIPrintInfoUIPrintInteractionController显式打印Web视图内容,但这将是一个单独的工具栏按钮,而我想简单地将系统的Print选项包含在activity按钮行中。我假设打印网页视图不需要任何明确的编码。

我该怎么办?

1 个答案:

答案 0 :(得分:1)

您可以像这样为UIActivityCotnroller创建自定义活动

 import UIKit

protocol CustomActivityDelegate : NSObjectProtocol
{
    func performActionCompletion(actvity: CustomActivity)
}


class CustomActivity: UIActivity {

    var delegate: CustomActivityDelegate?


    override class var activityCategory: UIActivityCategory {
        return .action
    }

    override var activityType: UIActivityType? {
        guard let bundleId = Bundle.main.bundleIdentifier else {return nil}
        return UIActivityType(rawValue: bundleId + "\(self.classForCoder)")
    }

    override var activityTitle: String? {
        return "You title"
    }

    override var activityImage: UIImage? {
        return <Your activity image >
    }

    override func canPerform(withActivityItems activityItems: [Any]) -> Bool {
        return true
    }

    override func prepare(withActivityItems activityItems: [Any]) {
        //
    }

    override func perform() {
        self.delegate?.performActionCompletion(actvity: self)
        activityDidFinish(true)
    }
}

您可以将此活动初始化为此类

let customActivity = CustomActivity()
customActivity.delegate = self

您可以在准备UIActivityController时添加此自定义活动

let activityViewController : UIActivityViewController = UIActivityViewController(activityItems: [customActivity], applicationActivities: nil)

您还需要实现回调方法

func performActionCompletion(actvity: CustomActivity)
{
   //Perform you task
}

注意:这只是伪代码,可能包含错误或语法问题