使用 NSPerformService API 调用连续互通相机服务



我正在尝试使用BOOL NSPerformService(NSString *itemName, NSPasteboard *pboard); API 以编程方式调用连续互通相机 (Mac OS) 服务,以便该功能可以挂在简单的按钮单击后面。需要作为参数传入的连续互通相机服务的名称是什么itemName

我无法从com.apple.nsserivcescache.plist文件中找到服务的名称,尽管从上下文菜单中,服务的名称是"拍照"和"扫描文档"。我不确定这些名称是否有效,因为它们始终与设备名称相关联(iPhone|iPad)。

我尝试过的事情。

NSPerformSerice( @"Take Photo", [NSPasteboard generalPasteboard] ); NSPerformSerice( @"<Name of the iPhone> Take Photo", [NSPasteboard generalPasteboard] ); NSPerformSerice( @"<Name of the iPhone>/Take Photo", [NSPasteboard generalPasteboard] );

我写了一篇关于将连续互通相机支持添加到您自己的应用程序的简短文章: https://thomas.zoechling.me/journal/2018/10/Continuity.html

您必须实现NSServicesMenuRequestor以指示您可以处理粘贴板中的图像:

override func validRequestor(forSendType sendType: NSPasteboard.PasteboardType?, returnType: NSPasteboard.PasteboardType?) -> Any? {
    if let pasteboardType = returnType,
        NSImage.imageTypes.contains(pasteboardType.rawValue) {
        return self
    } else {
        return super.validRequestor(forSendType: sendType, returnType: returnType)
    }
}

通过实现上述方法,当前第一响应者的菜单(例如按钮的菜单)将自动填充连续性相机菜单项。

该菜单的项将启动 CC UI,然后在用户执行捕获时调用readSelection(from: pasteboard)
您可以从那里读取粘贴板内容:

func readSelection(from pasteboard: NSPasteboard) -> Bool {
    guard pasteboard.canReadItem(withDataConformingToTypes: NSImage.imageTypes) else { return false }
    guard let image = NSImage(pasteboard: pasteboard) else { return false }
    self.imageView.image = image
    return true
}

还应该可以控制插入与 CC 相关的菜单项的位置。有一个相关的NSMenuItemImportFromDeviceIdentifier常量,但我还没有弄清楚如何使用它。(此推特帖子中的一些上下文:https://twitter.com/weichsel/status/1052980223891972096)

最新更新