簡體   English   中英

我怎樣才能找到一個知道它的 bundle-Identifier 的 NSBundle? (例如“com.apple.Preview”)

[英]How can I locate an NSBundle knowing its bundle-Identifier? (e.g. "com.apple.Preview")

我需要提取我知道其捆綁標識符但不知道其安裝位置的應用程序(和其他捆綁包)的可執行路徑

出於某種原因,[NSBundle bundleWithIdentifier:] 方法對我使用的任何標識符都失敗了。 無論是我的,Apple 的還是第 3 方。

NSBundle *appBundle = [NSBundle bundleWithIdentifier:@"com.apple.Preview"];
if (appBundle)
   NSLog(@"Bundle executable: %@", appBundle.executablePath);
else
   NSLog(@"No bundle for identifier: %@", identifier); 
    
appBundle = [NSBundle bundleWithPath: @"/System/Applications/Preview.app"];
if (appBundle)
   NSLog(@"Bundle executable: %@", appBundle.executablePath);
else
   NSLog(@"No bundle for path: %@", path);

Output 看起來像:

2022-11-28 16:51:19.04 TesterApp[2802:96004] No bundle for identifier: com.apple.Preview
2022-11-28 16:51:40.88 TesterApp[2802:96004] Bundle executable path: /System/Applications/Preview.app/Contents/MacOS/Preview

我嘗試了一切。 以 root 身份運行,在運行之前為我的 TesterApp 可執行文件提供“完全磁盤訪問”權限。 從 Apple Preview.app 所在的同一目錄 (/Applications) 運行 - 無濟於事。

只是不能使bundleWithIdentifier:工作。

雪上加霜—— 方法文檔中有兩行相互矛盾的內容

返回值

帶有包標識符標識符的 NSBundle object,如果在系統上找不到請求的包,則為 nil。

如果沒有與標識符關聯的現有包,則此方法創建並返回一個新的 NSBundle object。 否則,返回現有實例。

現在任何人都可以解釋 - 當在系統上找不到具有給定標識符的包時,該方法應該返回什么?

nil還是新的 bundle object?

我看到了一些相關的問題,但實際上沒有人試圖通過它的 bundle-id 在硬盤驅動器上找到一個包。

謝謝!

NSBundle文檔不是很清楚,因為bundleWithIdentifier方法有幾個段落列出了三個結果(現有包、新 object 或 nil),只有兩種可能性(包存在或不存在)。 該方法似乎僅在應用程序中有效,如果未找到包標識符,則返回 nil。

使用非沙盒應用程序進行測試, LSCopyApplicationURLsForBundleIdentifier和 NSWorkspace 的URLForApplicationWithBundleIdentifier適用於應用程序,但至少可以從名稱中推斷出來。 使用 NSBundle 的bundleWithPath似乎達不到目的,因為您已經有了路徑。

然而,在 Spotlight 元數據中搜索匹配的kMDItemCFBundleIdentifier似乎確實有效。 mdfind shell 實用程序或NSMetadataQuery使用來自應用程序、內核擴展、組件和我嘗試過的大多數腳本包的包標識符,盡管它確實取決於被索引的特定位置(以及具有Info.plist的包)。 然后可以使用結果中的路徑在/Contents/MacOS/手動構建可執行文件路徑。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM