【问题标题】:How to properly send IOSurface to another process using XPC?如何使用 XPC 正确地将 IOSurface 发送到另一个进程?
【发布时间】:2021-03-10 11:20:17
【问题描述】:

我正在开发一个 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 中支持的类型,所以我理解它应该可以工作。

【问题讨论】:

    标签: swift macos xpc iosurface


    【解决方案1】:

    当您初始化连接时,您必须将传入的类列入白名单。

    例如:

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

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

    【讨论】:

    • 你可以做let classSet: Set&lt;AnyHashable&gt; = [NSArray.self, NSDictionary.self]
    猜你喜欢
    • 1970-01-01
    • 2011-07-21
    • 1970-01-01
    • 1970-01-01
    • 2012-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-16
    相关资源
    最近更新 更多