Terminate a process by Cocoa

心不动则不痛 提交于 2019-12-04 11:18:42
Ken Aspeslagh

-[NSWorkspace runningApplications] returns an array of NSRunningApplication objects. NSRunningApplication has a method -[NSRunningApplication terminate]. So if you are looking for a specific application, you could terminate it like this:

-(void)killProcessesNamed:(NSString*)appName
{
    for ( id app in [[NSWorkspace sharedWorkspace] runningApplications] ) 
    {
        if ( [appName isEqualToString:[[app executableURL] lastPathComponent]] ) 
        {
            [app terminate];
        }
    }
}

You could also call forceTerminate to force the app to quit without the normal quitting process. (It won't ask to save changes, etc.)

There are other methods of NSRunningApplication that you can use to make this process simpler depending on whether or not you're searching for a process based on bundle ID or PID.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!