Julien Pilet's answer worked for me. However, to get it to not constantly relaunch the app when the device is still connected when closing the app, I had to:
- call
xpc_set_event_stream_handler()
in my app delegate applicationDidFinishLaunching
:
xpc_set_event_stream_handler("com.apple.iokit.matching", NULL, ^(xpc_object_t event) {
// Every event has the key XPC_EVENT_KEY_NAME set to a string that
// is the name you gave the event in your launchd.plist.
const char *name = xpc_dictionary_get_string(event, XPC_EVENT_KEY_NAME);
// IOKit events have the IORegistryEntryNumber as a payload.
uint64_t id = xpc_dictionary_get_uint64(event, "IOMatchLaunchServiceID");
// Reconstruct the node you were interested in here using the IOKit
// APIs.
NSLog(@"Received event: %s: %llu",name,id);
});
- add KeepAlive/false key/value pair to the plist
- add
IOMatchLaunchStream/true
key/value pair to the com.apple.device-attach
dict in the plist. This is in addition to the IOMatchStream
key already there. Not sure why that has to be there, I found a reference to it here: http://asciiwwdc.com/2013/sessions/702
Also don't forget to register the plist with the system using
launchctl load <path to your plist>
Note that this seems to work, but I never get the NSLog message from the xpc stream handler.