【问题标题】:Relaunching application via NSTask ignores LSMinimumSystemVersionByArchitecture通过 NSTask 重新启动应用程序会忽略 LSMinimumSystemVersionByArchitecture
【发布时间】:2012-08-07 18:13:48
【问题描述】:

我在 10.5 上重新启动我的应用程序时遇到了问题。在我的 Info.plist 中,我设置了 LSMinimumSystemVersionByArchitecture,以便应用程序在 x86_64 上以 64 位运行,在 i386、ppc 和 ppc64 上以 32 位运行。

我在应用程序中有一个首选项,允许用户在 Dock 图标和 NSStatusItem 之间切换,并提示用户使用以下代码更改设置后重新启动应用程序:

id fullPath = [[NSBundle mainBundle] executablePath];
NSArray *arg = [NSArray arrayWithObjects:nil];    
[NSTask launchedTaskWithLaunchPath:fullPath arguments:arg];
[NSApp terminate:self];

当它在 10.5 上执行时,它会以 64 位重新启动应用程序,这对我来说不是想要的结果。从我收集阅读文档的内容来看,因为当应用程序通过命令行启动时不会读取 LS* 键。

有没有办法解决这个问题?我尝试做类似下面的事情,它在 10.6 上有效,但在 10.5 上,它对我说“无法访问启动路径”。 ([NSApp isOnSnowLeopardOrBetter] 是一个检查 AppKit 版本号的类别)。

id path = [[NSBundle mainBundle] executablePath];    
NSString *fullPath = nil;
if (![NSApp isOnSnowLeopardOrBetter])      
  fullPath = [NSString stringWithFormat:@"/usr/bin/arch -i386 -ppc %@", path];
else    
  fullPath = path;

NSArray *arg = [NSArray arrayWithObjects:nil];    
[NSTask launchedTaskWithLaunchPath:fullPath arguments:arg];
[NSApp terminate:self]; 

【问题讨论】:

  • 你得到“启动路径不可访问”,因为“/usr/bin/arch -i386 -ppc /Applications/YourApp.app/Contents/MacOS/YourApp”不是可执行文件的路径你的系统。 NSTask 不使用外壳;您需要将要运行的可执行文件的路径 (/usr/bin/arch) 和参数列表传递给它单独,参数也彼此分开,收集在 NSArray 中。

标签: objective-c cocoa nstask


【解决方案1】:

您应该改用NSWorkspace 的方法,它确实考虑了Info.plist 键。例如,使用-(BOOL)launchApplication:(NSString*)

【讨论】:

    【解决方案2】:

    这是因为您在完整路径中使用空格,在数组[NSArray arrayWithObjects:@"/usr/bin/arch",@"-i386",@"-ppc",path,nil] 中使用参数。

    【讨论】:

      【解决方案3】:

      尝试使用此代码重新启动您的应用:

      //terminate your app in some of your method:
      [[NSApplication sharedApplication]terminate:nil];
      
      - (void)applicationWillTerminate:(NSNotification *)notification {
      
          if (i need to restart my app) {
      
             NSTask *task = [NSTask new];
             [task setLaunchPath:@"/usr/bin/open"];
             [task setArguments:[NSArray arrayWithObjects:@"/Applications/MyApp.app", nil]];
             [task launch];
         }
      }
      

      【讨论】:

        猜你喜欢
        • 2013-03-17
        • 2015-06-01
        • 1970-01-01
        • 1970-01-01
        • 2018-01-16
        • 2013-02-28
        • 2017-10-11
        • 2011-11-04
        • 1970-01-01
        相关资源
        最近更新 更多