There is a LoginItem in my app that launches the application. It works fine with approximately the following code snippet:
Since 11.0, launchApplicationAtURL: is deprecated in favour of openApplicationAtURL:. When I try to use it instead, the application does not start at all and no error is reported. What am I doing wrong? The new code looks like this:
Code Block NSBundle* bundle = NSBundle.mainBundle; NSWorkspace* workspace = NSWorkspace.sharedWorkspace; NSString* path = bundle.bundlePath; for (int i = 0; i < 4; ++i) { path = [path stringByDeletingLastPathComponent]; } NSDictionary* configuration = @{ NSWorkspaceLaunchConfigurationEnvironment: env }; NSError* error = nil; [workspace launchApplicationAtURL: [NSURL fileURLWithPath: path] options: NSWorkspaceLaunchDefault configuration: configuration error: &error]; if (error) { NSLog(@"Failed to run the app: %@", error.localizedDescription); }
Since 11.0, launchApplicationAtURL: is deprecated in favour of openApplicationAtURL:. When I try to use it instead, the application does not start at all and no error is reported. What am I doing wrong? The new code looks like this:
Code Block NSWorkspaceOpenConfiguration* configuration = [NSWorkspaceOpenConfiguration configuration]; [configuration setEnvironment: env]; [configuration setPromptsUserIfNeeded: YES]; [workspace openApplicationAtURL: [NSURL fileURLWithPath: path] configuration: configuration completionHandler:^(NSRunningApplication* app, NSError* error) { if (error) { NSLog(@"Failed to run the app: %@", error.localizedDescription); } }];
Figured it out. The login item quits too soon. It should wait for the open operation to be completed. Something like (not the best solution, for illustrative purpose only):
Code Block [workspace openApplicationAtURL: [NSURL fileURLWithPath: path] configuration: configuration completionHandler:^(NSRunningApplication* app, NSError* error) { if (error) { NSLog(@"Failed to run the app: %@", error.localizedDescription); } exit(0); }]; [NSThread sleepForTimeInterval: 10];