NSSharingService.perform() 与发送到照片
NSSharingService.perform() with Send To Photos
在 MacOS(Catalyst 应用程序,但 AppKit 捆绑包)上,我在主应用程序菜单中从菜单委托“动态”创建共享子菜单,如下所示:
func menuWillOpen(_ menu: NSMenu) {
self.provider = NSItemProvider(contentsOf: url)! //url points to existing temporary file of type .png
menu.removeAllItems()
let srvcs = NSSharingService.sharingServices(forItems: [self.provider!])
for srv in srvcs {
let it = NSMenuItem(title: srv.menuItemTitle, action: #selector(openSharingService), keyEquivalent: "")
it.image = srv.image
it.representedObject = srv
it.target = self
menu.addItem(it)
}
}
@objc private func openSharingService(sender: NSMenuItem) {
let service = (sender.representedObject as! NSSharingService)
service.perform(withItems: [self.provider!])
}
它适用于任何共享类型,但“发送到照片”除外。使用“发送到照片”时,我在控制台中收到此错误:
2021-10-27 08:59:02.042220+0200 Calculator2[14383:7732689] [xpc.exceptions] <NSXPCConnection: 0x6000008470c0> connection to service on pid 14388 named com.apple.share.System.add-to-iphoto.apple-extension-service: Exception caught during decoding of received selector _completeRequestReturningItems:forExtensionContextWithUUID:completion:, dropping incoming message.
Exception: Exception while decoding argument 0 (#2 of invocation):
Exception: value for key 'NS.objects' was of unexpected class 'NSURL (0x7fff801889e8) [/System/Library/Frameworks/CoreFoundation.framework]'. Allowed classes are '{(
"NSDate (0x7fff80188600) [/System/Library/Frameworks/CoreFoundation.framework]",
"NSString (0x7fff801ba8d0) [/System/Library/Frameworks/Foundation.framework]",
"NSNumber (0x7fff801ba3a8) [/System/Library/Frameworks/Foundation.framework]",
"NSData (0x7fff801885d8) [/System/Library/Frameworks/CoreFoundation.framework]",
"NSDictionary (0x7fff80188650) [/System/Library/Frameworks/CoreFoundation.framework]",
"NSArray (0x7fff80188510) [/System/Library/Frameworks/CoreFoundation.framework]"
)}'.
照片应用程序已打开并添加了图像,但我的应用程序收到上述错误并且其菜单全部变灰,直到重新启动。我是不是做错了什么?
编辑:我试过这样调用服务(传递 NSImage):
self.provider!.loadDataRepresentation(forTypeIdentifier: "public.png", completionHandler: {(data, error) in
let img = NSImage(data: data!)
DispatchQueue.main.async {
service.perform(withItems: [img])
}
})
... 它失败并出现完全相同的错误。再次抱怨 NSURL,即使我根本不通过 NSURL。
所以我找不到错误,但我设法解决了这个问题。我将以下代码添加到我的 func openSharingService() 中,以便识别添加到照片并以不同的方式处理它:
if ( (service == NSSharingService(named: .addToIPhoto)) && (self.provider!.hasItemConformingToTypeIdentifier("public.png")) ) {
self.provider!.loadObject(ofClass: NSURL.self, completionHandler: {(val, error) in
//print(val)
DispatchQueue.main.async {
let u = val as! NSURL
self.photosHandler(u)
NSWorkspace.shared.launchApplication("Photos")
}
})
}
self.photosHandler 是从我的催化剂应用程序包中填充的回调,它使用 PHPhotoLibrary 添加照片:
PHPhotoLibrary.requestAuthorization(for: .addOnly, handler: self.OnAuthorizationDone)
(OnAuthorizationDone() 也必须使用 PHPhotoLibrary.shared().performChanges()
和 PHAssetChangeRequest.creationRequestForAssetFromImage()
实现)
在 MacOS(Catalyst 应用程序,但 AppKit 捆绑包)上,我在主应用程序菜单中从菜单委托“动态”创建共享子菜单,如下所示:
func menuWillOpen(_ menu: NSMenu) {
self.provider = NSItemProvider(contentsOf: url)! //url points to existing temporary file of type .png
menu.removeAllItems()
let srvcs = NSSharingService.sharingServices(forItems: [self.provider!])
for srv in srvcs {
let it = NSMenuItem(title: srv.menuItemTitle, action: #selector(openSharingService), keyEquivalent: "")
it.image = srv.image
it.representedObject = srv
it.target = self
menu.addItem(it)
}
}
@objc private func openSharingService(sender: NSMenuItem) {
let service = (sender.representedObject as! NSSharingService)
service.perform(withItems: [self.provider!])
}
它适用于任何共享类型,但“发送到照片”除外。使用“发送到照片”时,我在控制台中收到此错误:
2021-10-27 08:59:02.042220+0200 Calculator2[14383:7732689] [xpc.exceptions] <NSXPCConnection: 0x6000008470c0> connection to service on pid 14388 named com.apple.share.System.add-to-iphoto.apple-extension-service: Exception caught during decoding of received selector _completeRequestReturningItems:forExtensionContextWithUUID:completion:, dropping incoming message.
Exception: Exception while decoding argument 0 (#2 of invocation):
Exception: value for key 'NS.objects' was of unexpected class 'NSURL (0x7fff801889e8) [/System/Library/Frameworks/CoreFoundation.framework]'. Allowed classes are '{(
"NSDate (0x7fff80188600) [/System/Library/Frameworks/CoreFoundation.framework]",
"NSString (0x7fff801ba8d0) [/System/Library/Frameworks/Foundation.framework]",
"NSNumber (0x7fff801ba3a8) [/System/Library/Frameworks/Foundation.framework]",
"NSData (0x7fff801885d8) [/System/Library/Frameworks/CoreFoundation.framework]",
"NSDictionary (0x7fff80188650) [/System/Library/Frameworks/CoreFoundation.framework]",
"NSArray (0x7fff80188510) [/System/Library/Frameworks/CoreFoundation.framework]"
)}'.
照片应用程序已打开并添加了图像,但我的应用程序收到上述错误并且其菜单全部变灰,直到重新启动。我是不是做错了什么?
编辑:我试过这样调用服务(传递 NSImage):
self.provider!.loadDataRepresentation(forTypeIdentifier: "public.png", completionHandler: {(data, error) in
let img = NSImage(data: data!)
DispatchQueue.main.async {
service.perform(withItems: [img])
}
})
... 它失败并出现完全相同的错误。再次抱怨 NSURL,即使我根本不通过 NSURL。
所以我找不到错误,但我设法解决了这个问题。我将以下代码添加到我的 func openSharingService() 中,以便识别添加到照片并以不同的方式处理它:
if ( (service == NSSharingService(named: .addToIPhoto)) && (self.provider!.hasItemConformingToTypeIdentifier("public.png")) ) {
self.provider!.loadObject(ofClass: NSURL.self, completionHandler: {(val, error) in
//print(val)
DispatchQueue.main.async {
let u = val as! NSURL
self.photosHandler(u)
NSWorkspace.shared.launchApplication("Photos")
}
})
}
self.photosHandler 是从我的催化剂应用程序包中填充的回调,它使用 PHPhotoLibrary 添加照片:
PHPhotoLibrary.requestAuthorization(for: .addOnly, handler: self.OnAuthorizationDone)
(OnAuthorizationDone() 也必须使用 PHPhotoLibrary.shared().performChanges()
和 PHAssetChangeRequest.creationRequestForAssetFromImage()
实现)