xcode - XPC remoteObjectWithErrorHandler 不会抛出错误

标签 xcode swift macos xpc

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

    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)
    }

最佳答案

虽然我遇到了类似的问题并且还没有解决它,但我想我可以帮助解决你在这里遗漏的问题。

连接对象可能会在没有任何投诉的情况下创建,即使您没有安装 XPC 服务。它也可以顺利恢复。实际的连接尝试仅在您尝试发送第一条消息时发生

那么……你的

connection.invalidationHandler = {
    self.helperToolConnection = nil
}

将在那时调用,使连接对象无效。出于这个原因,您的后续代码根本不执行任何操作 - 因为它的所有消息都发送到 nil。

为了验证我的理论 - 只需在使 invalidationHandler 中的 helperToolConnection 无效之前添加一个 NSLog(),然后重试。

关于xcode - XPC remoteObjectWithErrorHandler 不会抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35303911/

相关文章:

c - 安装了 XCode,但 GCC 不起作用

xcode - 添加使用 IB 构建的 UINavigationController

java - 在类中实现委托(delegate)(iOS/Swift)

Xcode 6 : Sprite node does not come in contact with other sprite node when tapped? 掉出屏幕?

c++ - 在 MacOS 上编译动态库,库搜索路径指向同一目录(CMake)

MACOSX : how to start daemon which need root privileges from cocoa application

iphone - 拖动特定的 UIImageView

ios - WatchKit 应用提交错误

swift - 使用默认初始化程序与方便初始化程序进行初始化

macos - NSView subview 右键单击无法在 nsimageview 上工作