3

我正在尝试检测是否通过错误处理程序安装了辅助工具,但错误块不会被执行,成功块也不会执行。当助手已经安装时,它工作正常。它只是不会在有错误时捕获错误。在文档中,这两个块中的一个总是被执行

    if helperToolConnection == nil {
        let connection = NSXPCConnection(machServiceName: "**bundle identifier**", options: NSXPCConnectionOptions.Privileged)
        connection.remoteObjectInterface = NSXPCInterface(withProtocol: HelperProtocol.self)
        connection.invalidationHandler = {
            self.helperToolConnection = nil
        }
        connection.resume()
        helperToolConnection = connection
    }

    let helper = helperToolConnection!.remoteObjectProxyWithErrorHandler() { error in
        NSLog("Failed to connect: \(error)")
        withReply(nil)
    } as! HelperProtocol
    helper.connectWithEndpointReply() { endpoint -> Void in
        withReply(endpoint)
    }
4

1 回答 1

0

尽管我遇到了类似的问题并且尚未解决,但我想我可以帮助您解决这里错过的一些问题。

即使没有安装 XPC 服务,也可以毫无问题地创建连接对象。它也可以顺利恢复。实际的连接尝试在您尝试发送第一条消息时发生。

所以……你的

connection.invalidationHandler = {
    self.helperToolConnection = nil
}

届时将被调用,使连接对象无效。出于这个原因,您的后续代码什么都不做 - 因为它的所有消息都发送到 nil。

为了验证我的理论 - 只需在您取消 invalidationHandler 之前添加一个 NSLog() helperToolConnection,然后再试一次。

于 2021-01-18T09:13:26.970 回答