我的方法是只使用一个在代码中加载的琐碎 VC,然后 VC 包含来实际呈现我们想要的 VC(从 Storyboard 加载)
info.plist 包含:
<key>NSExtension</key>
<dict>
<key>NSExtensionPrincipalClass</key>
<string>$(PRODUCT_MODULE_NAME).PreviewVCLoader</string>
<key>NSExtensionAttributes</key>
<dict>
<key>QLSupportsSearchableItems</key>
<true/>
<key>QLSupportedContentTypes</key>
<array>
<string>com.hobbyistsoftware.jump</string>
</array>
</dict>
<key>NSExtensionPointIdentifier</key>
<string>com.apple.quicklook.preview</string>
</dict>
然后我的 PreviewVCLoader 类是
import UIKit
import QuickLook
class PreviewVCLoader: UIViewController, QLPreviewingController {
init() {
super.init(nibName: nil, bundle: nil)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
lazy var realVC:PreviewViewController = {
let board = UIStoryboard.init(name: "MainInterface", bundle: nil)
guard let vc = board.instantiateViewController(withIdentifier: "PreviewViewController") as? PreviewViewController else {
fatalError("Unable to instantiate storyboard in preview")
}
return vc
}()
override func viewDidLoad() {
super.viewDidLoad()
self.addChild(realVC)
realVC.view.frame = self.view.bounds
realVC.view.autoresizingMask = [.flexibleWidth,.flexibleHeight]
self.view.addSubview(realVC.view)
realVC.didMove(toParent: self)
}
func preparePreviewOfFile(at url: URL, completionHandler handler: @escaping (Error?) -> Void) {
realVC.preparePreviewOfFile(at: url, completionHandler: handler)
}
}