如何使用 XPC 将 IOSurface 正确发送到另一个进程?

How to properly send IOSurface to another process using XPC?

我正在开发一个 MacOS 应用程序,我在其中尝试将 IOSurface 从 XPC 服务发送到客户端应用程序(与 XPC 服务在同一个项目中),但我显然缺少一些东西.我收到以下错误:

Exception: Attempt to decode an xpc type but whitelist does not specify an XPC type '<no key>'.

整个错误文本如下:

2021-03-10 12:00:42.014421+0100 My Project[82813:1358040] [xpc.exceptions] 
  <NSXPCConnection: 0x600001aae6c0> connection on anonymousListener or serviceListener from pid 82827: 
  Exception caught during decoding of received selector publishNewFrame:, dropping incoming message.
Exception: Exception while decoding argument 0 (#2 of invocation):
Exception: Attempt to decode an xpc type but whitelist does not specify an XPC type '<no key>'.

我正在使用以下代码发送表面:

guard let connection = processConnection else {
    print("Process is not available")
    return
}

let process = connection.remoteObjectProxyWithErrorHandler {
    error in print("remote proxy error: ", error)
} as! MyProtocol
    
let XPCSurfaceRef = IOSurfaceCreateXPCObject(unsafeBitCast(surface, to: IOSurfaceRef.self))
process.publishNewFrame(XPCSurfaceRef)

协议:

@objc public protocol MyProtocol: NSObjectProtocol {
  func publishNewFrame(_ XPCSurfaceRef: xpc_object_t)
}

在客户端进程上:

func publishNewFrame(_ XPCSurfaceRef: xpc_object_t) {
    let surfaceRef = IOSurfaceLookupFromXPCObject(XPCSurfaceRef)
}

我不知道如何继续。我不清楚我究竟需要将什么列入白名单,或者如何去做。原则上,IOSurface 是 XPC 中支持的类型,所以我知道它应该可以正常工作。

当您初始化连接时,您必须将传入的 类.

列入白名单

例如:

let classSet: Set<AnyHashable> = [NSArray.self, NSDictionary.self]
connection.remoteObjectInterface?.setClasses( classSet, for: #selector(XpcServiceProtocol.article(pzn:withReply:)), argumentIndex: 0, ofReply: true)

有时找到正确的 Class 有点棘手,因为 Swift 和 Objective-C 类 有时不一样。 您当然可以使用您的自定义 Class