您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 使用技巧 >> iOS 技巧大全

利用私有 API 獲取 iOS 設備上所有已安裝應用信息

編輯:iOS 技巧大全

在 iOS 開發中,若需要讀取設備上所有已安裝應用的相關信息(包括版本、名稱、Bundle ID、類型等),可以借助系統私有庫 MobileCoreService 中的私有類 LSApplicationWorkspace 來實現。

原理說明

LSApplicationWorkspace 提供了 -(id)allInstalledApplications 方法,它能取得設備上所有應用(包含系統應用與用戶安裝的應用)的信息。返回的每個元素都是一個 LSApplicationProxy 對象,透過它能取得 App 的版本、名稱、Bundle ID 與類型等屬性。

由於這些都是私有方法,我們可以透過 Objective-C 的運行時(Runtime)機制來動態調用。首先需要引入頭文件:

#include <objc/runtime.h>

實現代碼

- (void)getAllApps { //獲取手機上所有的app Class LSApplicationWorkspace_class = objc_getClass("LSApplicationWorkspace"); NSObject *workspace = [LSApplicationWorkspace_class performSelector:@selector(defaultWorkspace)]; NSArray *apps = [workspace performSelector:@selector(allInstalledApplications)]; Class LSApplicationProxy_class = objc_getClass("LSApplicationProxy"); for (int i = 0; i < apps.count; i++) { NSObject *temp = apps[i]; if ([temp isKindOfClass:LSApplicationProxy_class]) { //應用的bundleId NSString *appBundleId = [temp performSelector:NSSelectorFromString(@"applicationIdentifier")]; //應用的名稱 NSString *appName = [temp performSelector:NSSelectorFromString(@"localizedName")]; //應用的類型是系統的應用還是第三方的應用 NSString * type = [temp performSelector:NSSelectorFromString(@"applicationType")]; //應用的版本 NSString * shortVersionString = [temp performSelector:NSSelectorFromString(@"shortVersionString")]; NSLog(@"類型=%@應用的BundleId=%@ ++++應用的名稱=%@版本號=%@",type,appBundleId,appName,shortVersionString); } } }

注意事項

需要提醒的是,這些屬於私有 API,若應用於正式上架的 App,可能會面臨 App Store 審核被拒的風險,因此僅建議用於學習研究或內部工具。

如果想查看 MobileCoreServices 裡的完整頭文件,可以參考這個開源倉庫:https://github.com/nst/iOS-Runtime-Headers/tree/master/Frameworks/MobileCoreServices.framework。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved