Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
bundlePath在MacOSX 10.5上作为Launch Deamon运行时返回空_Macos_Cocoa - Fatal编程技术网

bundlePath在MacOSX 10.5上作为Launch Deamon运行时返回空

bundlePath在MacOSX 10.5上作为Launch Deamon运行时返回空,macos,cocoa,Macos,Cocoa,我有一个Cocoa命令行应用程序,它是针对10.5SDK构建的。在应用程序中,我有 NSString *appPath = [[NSBundle mainBundle] bundlePath]; NSLog(@"%@", appPath); 在MacOSX10.5上,当我从命令行运行应用程序时,我得到了路径的预期输出。但是,如果我将应用程序设置为作为LaunchDeamon运行,它只输出一个“/” 它在10.6和10.7上作为一个执事和一个应用程序都可以正常工作。有人知道为什么会有不同吗?有没

我有一个Cocoa命令行应用程序,它是针对10.5SDK构建的。在应用程序中,我有

NSString *appPath = [[NSBundle mainBundle] bundlePath];
NSLog(@"%@", appPath);
在MacOSX10.5上,当我从命令行运行应用程序时,我得到了路径的预期输出。但是,如果我将应用程序设置为作为LaunchDeamon运行,它只输出一个“/”

它在10.6和10.7上作为一个执事和一个应用程序都可以正常工作。有人知道为什么会有不同吗?有没有更好的方法来获取在10.5+上运行的应用程序路径

更新


对我来说,公认答案中的解决方案不起作用。然而,关于将“WorkingDirectory”键添加到LaunchDeamon的plist文件中的评论得到了解决。显然,这是Mac OS X 10.5所需要的,但不是10.6+。

感谢您回答我的澄清问题

NSBundle依赖于现有的捆绑包及其相关的Info.plists和捆绑包ID(例如
com.apple.textedit.app
)等

虽然单个二进制文件不是一个捆绑包,但我猜苹果工程部已经在10.6和10.7中安排了
[[NSBundle mainBundle]bundlePath]
来做“正确的事情”。但您仍然需要10.5的解决方案

也许UNIX库函数可以让您到达需要的位置

对于正确的解决方案,我建议使用如下代码执行运行时条件检查:

+ (NSString *) getAppPath
{
    NSString * appPath = NULL;
    SInt32  minorVersionNum;
    OSErr   err;

    err = Gestalt(gestaltSystemVersionMinor,&minorVersionNum);

    // do this only if we're running on anything *older* than 10.6
    if((noErr == err) && (minorVersionNumber < 6)) 
    {
        // hopefully the below define is enough space for any returned path
        #define kMaxPathLength 512 

        size_t bufferLength = kMaxPathLength;
        char bufferToHoldPath[kMaxPathLength]; 

        // initialize the buffer & guarantee null-terminated strings
        bzero(&bufferToHoldPath,bufferLength);
        if( getcwd(&bufferToHoldPath, bufferLength) != NULL)
        {
            appPath = [NSString stringWithUTF8String: bufferToHoldPath];
        }
    } 

    // this code runs for 10.6 and *newer*, and attempts it on
    // 10.5 only if the above getcwd call failed
    if(NULL == appPath)
    {
        appPath = [[NSBundle mainBundle] bundlePath];
    }

    return(appPath);
}
+(NSString*)getAppPath
{
NSString*appPath=NULL;
SInt32 minorVersionNum;
OSErr错误;
err=格式塔(格式塔系统版本Minor和minorVersionNum);
//只有在运行10.6版本之前的版本时,才能执行此操作
如果((noErr==err)&(minorVersionNumber<6))
{
//希望下面的定义对于任何返回的路径都有足够的空间
#定义kMaxPathLength 512
大小\u t bufferLength=kmaxathLength;
char bufferToHoldPath[kmaxathlength];
//初始化缓冲区并保证以null结尾的字符串
bzero(&bufferToHoldPath,bufferLength);
if(getcwd(&bufferToHoldPath,bufferLength)!=NULL)
{
appPath=[NSString stringWithUTF8String:bufferToHoldPath];
}
} 
//此代码适用于10.6和*更新版本*,并尝试在其上运行
//10.5仅当上述getcwd调用失败时
if(NULL==appPath)
{
appPath=[[NSBundle mainBundle]bundlePath];
}
返回(appPath);
}

在回答之前,我没有测试这段代码,所以在这里确认一下。。。这是一个单独的命令行应用程序,它与基础链接,而不是在应用程序包(即包内的内容、资源和MACOS文件夹)中传递的?这是正确的。它使用的是基础。h,是一个编译的二进制文件,不嵌入A。今天晚些时候我将测试它的实现。我想强调的一点是:bundlePath在10.5中可以工作,但前提是该进程不作为LaunchDeamon运行(即,键入./MyProc可以工作,但使用launchctl不能)。Michael-不幸的是,这不起作用。当从命令行(./MyTool)运行时,它按预期工作;但是,当作为LaunchDeamon运行时,它不会。它打印“无法获取工作目录”Hmmm。。。看看,特别是在“推荐行为”部分。。。可以通过在Info.plist中包含WorkingDirectory来设置工作目录。这里可能还有其他一些有用的提示。