How to detect whether an OS X application is already launched

前端 未结 9 1609
自闭症患者
自闭症患者 2020-12-14 02:46

Normally an application bundle on OS X can only be started once, however by simply copying the bundle the same application can be launched twice. What\'s the best strategy t

9条回答
  •  无人及你
    2020-12-14 02:46

    detect if application with same bundleID is running, activate it and close what starts.

    - (id)init method of < NSApplicationDelegate >
    
        NSArray *apps = [NSRunningApplication runningApplicationsWithBundleIdentifier:[[NSBundle mainBundle] bundleIdentifier]];
        if ([apps count] > 1)
        {
            NSRunningApplication *curApp = [NSRunningApplication currentApplication];
            for (NSRunningApplication *app in apps)
            {
                if(app != curApp)
                {
                    [app activateWithOptions:NSApplicationActivateAllWindows|NSApplicationActivateIgnoringOtherApps];
                    break;
                }
            }
            [NSApp terminate:nil];
            return nil;
        }
    

提交回复
热议问题