3

I'm trying to detect if helper tool is installed trough error handler, but the error block won't get executed neither the success block. When the helper is already installed it works fine. It just doesn't catch errors when there is one. In documentation is that always one of those two blocks get executed

    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)
    }
Dellar
  • 41
  • 4

1 Answers1

0

Although I struggle with similar issue and have NOT yet resolved it, I think I can help with something you've missed here.

The connection object may be created without any complaint, even when your XPC Service is not installed. It can also be resumed without a hitch. The actual connection attempt only happens when you try to send the first message.

So... your

connection.invalidationHandler = {
    self.helperToolConnection = nil
}

Will be called at that time, nullifying the connection object. For that reason, your subsequent code does nothing at all - because all its messages are sent to nil.

To verify my theory - simply add an NSLog() before you nullify the helperToolConnection in the invalidationHandler, and try again.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Motti Shneor
  • 2,095
  • 1
  • 18
  • 24