经过地狱般的搜索和试验后,我准备回答我自己的问题,这样其他人就可以节省时间和精力。
我的结论是,目前 HelperApp 无法通过 Sandbox 下的一些参数启动 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;
}
}