gpt4 book ai didi

cocoa - 了解 MainApp 是否由 HelperApp 启动

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

我真的被一个简单的任务困在这里,但仍然无法让它工作。

我已经成功使用 HelperApp 实现了“LaunchAtLogin”功能,如 this article 中所述。 。但我的应用程序应该对 launch-by-helper-applaunch-by-user 操作做出不同的 react 。所以我现在的任务是有某种标志表明 MainApp 是由 HelperApp 启动的。

我知道有很多类似的问题,但仍然没有一个解决方案适合我。沙盒似乎删除了我尝试发送到 MainApp 的所有参数。

这是我尝试过的:

- [NSWorkspace - launchApplicationAtURL: options: configuration: error:]
- [NSWorkspace - openURLs: withAppBundleIdentifier: options: additionalEventParamDescriptor: launchIdentifiers:]
- LSOpenApplication()

直到最近,我认为当用户手动启动应用程序时,我可以依赖 Finder 发送的 -psn 参数。但即使 MainApp 由 HelperApp 启动,此参数也会在 10.8 上发送。

article如上所述,作者建议使用[NSWorkspace - launchApplicationAtURL: options: configuration: error:]。参数已发送,但没有任何内容到达 MainApp。

有人成功完成此(或类似)任务吗?

需要帮助!提前致谢!

最佳答案

经过 hell 般的搜索和实验,我准备好回答我自己的问题,这样其他人就可以节省时间和精力。

我的结论是,目前 HelperApp 无法在沙箱下通过一些参数启动 MainApp。至少我还没有找到任何方法来做到这一点。

像这样启动MainApp:

[[NSWorkspace sharedWorkspace] launchApplication:newPath];

在MainApp中添加以下内容:

Application_IsLaunchedByHelperApp = YES;

ProcessSerialNumber currPSN;
OSStatus err = GetCurrentProcess(&currPSN);
if (!err)
{
// Get information about our process
NSDictionary * currDict = [(NSDictionary *)ProcessInformationCopyDictionary(&currPSN,
kProcessDictionaryIncludeAllInformationMask) autorelease];

// Get the PSN of the app that launched us. Its not really the parent app, in the unix sense.
long long temp = [[currDict objectForKey:@"ParentPSN"] longLongValue];

long long hi = (temp >> 32) & 0x00000000FFFFFFFFLL;
long long lo = (temp >> 0) & 0x00000000FFFFFFFFLL;
ProcessSerialNumber parentPSN = {(UInt32)hi, (UInt32)lo};

// Get info on the launching process
NSDictionary * parentDict = [(NSDictionary*)ProcessInformationCopyDictionary(&parentPSN,
kProcessDictionaryIncludeAllInformationMask) autorelease];

// analyze
// parent app info is not null ?
if (parentDict && parentDict.count > 0)
{
NSString * launchedByAppBundleId = [parentDict objectForKey:@"CFBundleIdentifier"];
if (![launchedByAppBundleId isEqualToString:HELPER_APP_BUNDLE_ID])
{
Application_IsLaunchedByHelperApp = NO;
}
}
}

就是这样。 Application_IsLaunchedByHelperApp 现在具有正确的值。

解决方案不是我的。我在网上找到了它(我猜是cocoabuilder)。祝大家好运!感谢您关注我的问题。

更新看起来有些情况下在登录应用程序启动时会显示 launchedByAppBundleId = @"com.apple.loginwindow"。所以代码的最后一部分将如下所示:

    //
// analyze
//

// parent app info is not null ?
if (parentDict && parentDict.count > 0)
{
NSString * launchedByAppBundleId = [parentDict objectForKey:@"CFBundleIdentifier"];
if (![launchedByAppBundleId isEqualToString:HELPER_APP_BUNDLE_ID] &&
![launchedByAppBundleId isEqualToString:@"com.apple.loginwindow"])
{
Application_IsLaunchedByHelperApp = NO;
}
}

关于cocoa - 了解 MainApp 是否由 HelperApp 启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15599345/

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