简体   繁体   中英

Killing Launch Daemon

My app runs a launch daemon so the main app can be started synchronically with iTunes. The daemon runs with no issues.

However, when I disable the daemon to start at login, the daemon is already launched and keeps opening the main app until the user restarts the computer and the daemon is shut down.

How can I terminate my daemon process? Because it's a process with no interface, the app is no NSRunningApplication .


EDIT

Worthy to say, the application will have to be sandboxed.

Ok, by removing the helper app from the daemon services list, the app automatically gets killed.


Code

- (void)setLaunchHelperAtLogin:(BOOL)launchHelperAtLogin {
    if (launchHelperAtLogin != self.launchHelperAtLogin) {
        // Creating helper app complete URL
        NSURL *bundleURL = [[NSBundle mainBundle] bundleURL];
        NSURL *url = [bundleURL URLByAppendingPathComponent:kHelperPath];

        // Registering helper app
        OSStatus status = LSRegisterURL((__bridge CFURLRef)url, true);
        if (status != noErr) {
            NSError *error = [NSError errorWithDomain:NSOSStatusErrorDomain code:status userInfo:nil];
            NSLog(@"%@", error);
        }

        // Setting login
        if (!SMLoginItemSetEnabled((CFStringRef)kHelperBundleIdentifier, launchHelperAtLogin)) {
            NSLog(@"SMLoginItemSetEnabled failed!");
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM