gpt4 book ai didi

macos - 辅助应用程序无法被终止并且未启动主应用程序

转载 作者:行者123 更新时间:2023-12-03 17:47:47 27 4
gpt4 key购买 nike

我还有其他关于在登录时启动沙盒应用程序的问题,显然没有解决方案:herehere

如您所知,您必须创建一个辅助应用程序来启动主应用程序并退出。

所有教程都说要将其添加到帮助器委托(delegate)中:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {

// Get the path for the main app bundle from the helper bundle path.
NSString *basePath = [[NSBundle mainBundle] bundlePath];
NSString *path = [basePath stringByDeletingLastPathComponent];
path = [path stringByDeletingLastPathComponent];
path = [path stringByDeletingLastPathComponent];
path = [path stringByDeletingLastPathComponent];

[[NSWorkspace sharedWorkspace] launchApplication:path];
[[NSApplication sharedApplication] terminate:self];
}

此代码完全不执行任何操作,因为主应用程序未启动(出于您将在我的其他问题中看到的原因),并且作为奖励,助手不会被杀死。

我尝试使用多种方法杀死助手,例如

[NSApp terminate:self];

甚至还有这种戏剧性的方法

NSArray *runningApplications = [[NSWorkspace sharedWorkspace] runningApplications];
NSString *theName;
NSNumber *pid;
for ( NSDictionary *applInfo in runningApplications ) {
if ( (theName = [applInfo objectForKey:@"NSApplicationName"]) ) {
if ( (pid = [applInfo objectForKey:@"NSApplicationProcessIdentifier"]) ) {
//NSLog( @"Process %@ has pid:%@", theName, pid );
if( [theName isEqualToString:@"MyHelper"] ) {
kill( [pid intValue], SIGKILL );
}
}
}
}

没有什么可以杀死助手。

作为另一个好处,当我手动启动主应用程序并且它位于菜单栏上时,我可以选择从主应用程序本身中选择QUIT,这样我就可以退出该应用程序,但使用相同的编程方法,主应用程序本身也是不可杀死的。

这是怎么回事?

我已按照@vadian 的说明进行操作,但它不起作用。 我已将概念验证项目上传到 here 。您将看到助手加载,但应用程序未加载。

最佳答案

您的方法无法工作,因为它不完整。至少你必须添加这两个路径组件来获取主应用程序可执行文件的路径(将 MyApp 替换为 MacOS 文件夹中可执行文件的名称)

path = [path stringByAddingPathComponent:@"MacOS"];
path = [path stringByAddingPathComponent:@"MyApp"];

尽管如此,推荐且可靠的方法是检查主应用程序是否已在运行并向主应用程序发送 LaunchConfigurationArgument 以指示应用程序在登录时启动(当然要使用NSBundleNSWorkspace 的现代 URL 相关 API):

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
BOOL alreadyRunning = NO, isActive = NO;
NSArray *running = [[NSWorkspace sharedWorkspace] runningApplications];
for (NSRunningApplication *app in running) {
if ([[app bundleIdentifier] isEqualToString:@"com.myCompany.MyApp"]) {
alreadyRunning = YES;
isActive = [app isActive];
break;
}
}

if (!alreadyRunning || !isActive) {
NSURL *bundleURL = [[NSBundle mainBundle] bundleURL];
NSMutableArray *pathComponents = [[bundleURL pathComponents] mutableCopy];
NSUInteger numberOfPathcomponents = [pathComponents count];
[pathComponents removeObjectsInRange:NSMakeRange(numberOfPathcomponents - 3, 3)];
[pathComponents addObject:@"MacOS"];
[pathComponents addObject:@"MyApp"];
NSURL *newURL = [NSURL fileURLWithPathComponents:pathComponents];
NSDictionary *dict = @{NSWorkspaceLaunchConfigurationArguments: @[@"launchedAtLogin"]};
[[NSWorkspace sharedWorkspace] launchApplicationAtURL:newURL
options:NSWorkspaceLaunchWithoutActivation
configuration:dict
error:nil];
}
[NSApp terminate:nil];
}

基本上,您不必从其他地方退出帮助程序应用程序。无论如何,帮助应用程序都应该自行退出。

关于macos - 辅助应用程序无法被终止并且未启动主应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45267606/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com