Ios 在MacCatalyst中使用AppKit和PDFKit 目标
动态加载macOS捆绑包以将PDFKit/AppKit使用到MacCatalyst应用程序中。然后,通过方法Ios 在MacCatalyst中使用AppKit和PDFKit 目标,ios,macos,mac-catalyst,Ios,Macos,Mac Catalyst,动态加载macOS捆绑包以将PDFKit/AppKit使用到MacCatalyst应用程序中。然后,通过方法PDFDocument().printOperation()在不显示对话框的情况下打印PDF 问题 我的函数printPDF(data:data)在MacOS应用程序中正常工作,但在MacCatalyst中崩溃。通过将“plugin”包加载到MacCatalyst应用程序,我可以很好地使用AppKit中的方法(通过以下步骤),但尝试从PDFKit打印PDF会引发异常PDFDocument
PDFDocument().printOperation()
在不显示对话框的情况下打印PDF
问题
我的函数printPDF(data:data)
在MacOS应用程序中正常工作,但在MacCatalyst中崩溃。通过将“plugin”包加载到MacCatalyst应用程序,我可以很好地使用AppKit中的方法(通过以下步骤),但尝试从PDFKit打印PDF会引发异常PDFDocument printOperationForPrintInfo:scalingMode:autoRotate:::::::发送到实例0x600000092C0的无法识别的选择器。我的生成设置中是否需要包含一些内容来解决此问题
最小可重复示例
创建MacCatalyst应用程序
创建MacOS捆绑包,并将其嵌入应用程序目标的“框架、库”部分
创建一个插件文件plugin.swift
,其目标成员身份是捆绑包和应用程序目标
创建一个文件MacOSPlugin.swift
,其目标成员资格仅为MacCatalyst应用程序
在AppDelegate中加载插件并调用printPDF()
//Plugin.swift
@objc (Plugin)
protocol Plugin:NSObjectProtocol {
init()
func sendEmail(recipients: [String], subject: String?, body: String)
func printPDF(data: Data)
}
//MacOSPlugin.swift
import Cocoa
import PDFKit
class MacOSPlugin:NSObject, Plugin {
required override init() { }
/// Open the device's default email browser and populate the to, subject, and body fields.
/// - Parameters:
/// - recipients: Email Recipients
/// - subject: Email Subject
/// - body: Email Text
func sendEmail(recipients: [String], subject: String?, body: String) {
let emailService = NSSharingService.init(named: NSSharingService.Name.composeEmail)!
emailService.recipients = recipients
emailService.subject = subject
emailService.perform(withItems: [body])
}
/// Opens the default printer and immediately starts a print job.
/// - Parameter data: PDF Data
func printPDF(data: Data) {
let info = NSPrintInfo.shared
//Magic numbers that put the pdf in the top right corner of the page for shipping.
info.rightMargin = 390
info.topMargin = 443
let document = PDFDocument(data: data)
let operation = document?.printOperation(
for: info,
scalingMode: .pageScaleNone,
autoRotate: false
)
operation?.showsPrintPanel = false
operation?.run()
}
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
testPlugin()
return true
}
func testPlugin() {
let plugin = loadPlugin(module: "MacOSPlugin", bundleName: "MacOSPlugin")
plugin?.printPDF(data: Data(base64Encoded: "JVBERi0xLjIgCjkgMCBvYmoKPDwKPj4Kc3RyZWFtCkJULyA5IFRmKFRlc3QpJyBFVAplbmRzdHJlYW0KZW5kb2JqCjQgMCBvYmoKPDwKL1R5cGUgL1BhZ2UKL1BhcmVudCA1IDAgUgovQ29udGVudHMgOSAwIFIKPj4KZW5kb2JqCjUgMCBvYmoKPDwKL0tpZHMgWzQgMCBSIF0KL0NvdW50IDEKL1R5cGUgL1BhZ2VzCi9NZWRpYUJveCBbIDAgMCA5OSA5IF0KPj4KZW5kb2JqCjMgMCBvYmoKPDwKL1BhZ2VzIDUgMCBSCi9UeXBlIC9DYXRhbG9nCj4+CmVuZG9iagp0cmFpbGVyCjw8Ci9Sb290IDMgMCBSCj4+CiUlRU9G")!)
}
func loadPlugin(module:String, bundleName:String) -> Plugin? {
let bundleFilename = "\(bundleName).bundle"
guard let bundleURL = Bundle.main.builtInPlugInsURL?
.appendingPathComponent(bundleFilename) else { return nil }
guard let bundle = Bundle(url: bundleURL) else { return nil }
guard let pluginClass = bundle.classNamed("\(module).\(bundleName)") as? Plugin.Type else { return nil }
return pluginClass.init()
}