2012-04-29 95 views
4

我有一個沙盒應用程序。我需要它在每次啓動時啓動一個輔助應用程序(從主應用程序的包中)。然而,這種失敗:從沙盒應用程序啓動助手

NSError *error; 
[[NSWorkspace sharedWorkspace] launchApplicationAtURL:helperURL 
           options:NSWorkspaceLaunchDefault 
           configuration:nil 
           error:&error]; 

的錯誤是:

The application 「Helper」 could not be launched because it is corrupt., NSUnderlyingError=0x10214c700 "The operation couldn’t be completed. (OSStatus error -10827.)"}

現在,錯誤是誤導性的,因爲如果我禁用沙盒授權的應用程序啓動的罰款。顯然這是一個錯誤,據報道here

我的問題是:是否有解決方法?

我可以用SMLoginItemSetEnabled,描述here

Pass true to start the helper application immediately and indicate that it should be started every time the user logs in. Pass false to terminate the helper application and indicate that it should no longer be launched when the user logs in.

但是,我不能使用這個API,而不要求用戶第一,因爲應用商店審查指南2.26:

Apps that are set to auto-launch or to have other code automatically run at startup or login without user consent will be rejected

因此,使用此解決方法意味着詢問用戶「每次登錄時啓動助手都可以?如果不是,則不能使用此應用程序!」顯然,這不是理想......

+0

不確定這是否與rdar:// 10934199相關,因爲launchApplicationAtURL在沙箱下失敗,即使它嘗試的應用程序啓動已由用戶手動啓動 – valexa 2012-05-08 19:34:07

+0

您是否可以獲得安裝用戶啓動代理的權限,該代理將檢查您的應用是否正在運行並啓動幫助程序? – Colin 2012-07-18 19:01:55

回答

2

一個可行的解決方法是使用NSTask產卵/usr/bin/open,並給它的助手應用程序的路徑:

NSTask *task = [NSTask new]; 
[task setLaunchPath: @"/usr/bin/open"]; 
[task setArguments: [NSArray arrayWithObjects: helperPath, nil]]; 
[task launch]; 

這運行正常從沙盒,並且似乎與兼容Mac App Store評論指南。

更新:經過進一步的調查,這項技術經常失敗,當我關掉沙盒不會發生此錯誤的錯誤

The application cannot be opened because its executable is missing.

。所以必須有更好的解決方案...

+0

在控制檯「xcrun:error:can not be used within a App Sandbox」中使用/ usr/bin/opendiff會產生以下錯誤。即使蘋果的指導方針聲明「系統自動允許沙盒應用程序」「讀取世界可讀的文件,在某些目錄中,包括以下目錄:」「/ usr/bin」...我想這不是真的了嗎? – 2015-04-11 14:37:31

0

你可以使用SMLoginItemSetEnabled。你必須要求用戶同意一次。畢竟,使用SMLoginItemSetEnabled首次啓動的幫助程序應用程序會在用戶每次登錄時自動啓動。

+0

不適合我。我有一個應用程序,只是在菜單欄中,並沒有窗口或碼頭的存在。 'SMLoginItemSetEnabled'將註冊助手,但助手無法啓動主應用程序。該應用程序是沙盒的應用程序商店。 – SpaceDog 2017-07-23 11:04:02