如何打印SFSafariViewController完整网页?

时间:2018-07-03 06:20:54

标签: ios swift sfsafariviewcontroller

我正在SFSafariViewController中打开一个pdf文件。现在,我要从SFSafariViewController打印此PDF。

但是Safari浏览器视图控制器的“共享”按钮没有打印选项。

请告诉我如何向SFSafariViewController添加打印功能。

1 个答案:

答案 0 :(得分:0)

您可以自定义活动。

    extension ViewController: SFSafariViewControllerDelegate {
        func safariViewController(_ controller: SFSafariViewController, activityItemsFor URL: URL, title: String?) -> [UIActivity] {
            let acitivity = CustomActivity()
            return [acitivity]
        }

        func printURL(url: URL) {
        }
    }

CustomActivity.swift

import Foundation
import UIKit

class CustomActivity: UIActivity {
    override class var activityCategory: UIActivityCategory {
        return .action
    }

    override var activityType: UIActivityType? {
        return UIActivityType.print
    }

    override var activityTitle: String? {
        return "Print"
    }

    override var activityImage: UIImage? {
        return nil
    }

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

    override func perform() {
        // do something here
    }

}

enter image description here