gpt4 book ai didi

objective-c - COCOA:用户登录时未启动应用程序帮助程序

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

请允许我详细说明:

目标

我希望我的应用程序助手在启动时启动(用户登录)

我在做什么

我正在使用 SMLoginItemSetEnabled 将我的应用程序助手设置为使用以下代码设置为登录项

if (!SMLoginItemSetEnabled ((__bridge CFStringRef)@"com.userName.LaunchAtLoginHelperApp", YES)) {
NSAlert *alert = [NSAlert alertWithMessageText:@"An error ocurred"
defaultButton:@"OK"
alternateButton:nil
otherButton:nil
informativeTextWithFormat:@"Couldn't add Helper App to launch at login item list."];
[alert runModal];
}

上面的代码是我在主应用程序的 applicationDidFinishLaunching 上完成的

现在,让我们来看看LaunchAtLoginHelperApp,在其applicationDidFinishLaunching上我有以下代码

BOOL alreadyRunning = NO;
BOOL isActive = NO; // my modification
NSArray *running = [[NSWorkspace sharedWorkspace] runningApplications];
for (NSRunningApplication *app in running) {

if ([[app bundleIdentifier] isEqualToString:@"com.userName.LaunchAtLoginApp"]) {
alreadyRunning = YES;
isActive = [app isActive];
}
}

if (!alreadyRunning || !isActive) {
NSString *path = [[NSBundle mainBundle] bundlePath];
NSArray *p = [path pathComponents];
NSMutableArray *pathComponents = [NSMutableArray arrayWithArray:p];
[pathComponents removeLastObject];
[pathComponents removeLastObject];
[pathComponents removeLastObject];
[pathComponents addObject:@"MacOS"];
[pathComponents addObject:@"LaunchAtLoginApp"];
NSString *newPath = [NSString pathWithComponents:pathComponents];
[[NSWorkspace sharedWorkspace] launchApplication:newPath];
}
[NSApp terminate:nil];

现在,让我们来看看我在这两个应用程序中所做的设置

LaunchAtLoginApp 设置

我已在构建阶段>>复制文件中复制了帮助程序应用程序 enter image description here

添加了ServiceManagement.framework

LaunchAtLoginHelperApp

我已将其设为仅后台应用程序并将其跳过安装设置为"is"

帮助应用程序已沙盒化

是的,我已将应用程序放入应用程序文件夹中,然后运行并重新启动系统,但应用程序没有自行启动。

请告诉我哪里做错了。另外,如果您能告诉我如何调试该问题,那就太好了。

P.S:我在主应用程序中使用了更多框架

 WebKit.framework
Sparkle.framework
QuartzCore.framework
SystemConfiguration.framework

最佳答案

我也遇到过同样的问题。对我来说,来自 Console.app 的日志非常有用。我不记得日志中的确切文本,但错误类似于:无法找到标识符的包...因此,您需要在 SMLoginItemSetEnabled 之前尝试将 url 注册到辅助应用程序。

LSRegisterURL((__bridge CFURLRef)urlToLoginHelper, true)
SMLoginItemSetEnabled(...)

似乎是苹果文档所要求的,但后来由于某些原因被删除了。看来现在你don't need register url for sandboxed apps 。但我还没有检查过这个建议。

关于objective-c - COCOA:用户登录时未启动应用程序帮助程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34896056/

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