1.獲取手機中安裝的全部App
1.1.runtime中的方法,因此要導入
#include <objc/runtime.h>
1.2.在 AppDelegate.m文件中的 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions方法中:
Class LSApplicationWorkspace_class = objc_getClass("LSApplicationWorkspace");
NSObject* workspace = [LSApplicationWorkspace_class performSelector:@selector(defaultWorkspace)];
NSArray *allApplications = [workspace performSelector:@selector(allApplications)];
//這樣就能獲取到手機中安裝的全部App
打印出來是這樣的:
2016-06-01 11:11:41.540 jqb[7537:2410265] app = <LSApplicationProxy: 0x1566a3c50> com.apple.mobilesms.notification <file:///Applications/MessagesNotificationViewService.app>
2016-06-01 11:11:41.541 jqb[7537:2410265] app = <LSApplicationProxy: 0x1566a5850> com.apple.mobilesms.compose <file:///Applications/MessagesViewService.app>
2016-06-01 11:11:41.541 jqb[7537:2410265] app = <LSApplicationProxy: 0x1566a60e0> com.apple.Fitness <file:///Applications/Fitness.app>
2016-06-01 11:11:41.541 jqb[7537:2410265] app = <LSApplicationProxy: 0x1566a6790> com.apple.Home.HomeUIService <file:///Applications/HomeUIService.app>
2016-06-01 11:11:41.541 jqb[7537:2410265] app = <LSApplicationProxy: 0x1566a6d60> com.apple.StoreDemoViewService <file:///Applications/StoreDemoViewService.app>
2016-06-01 11:11:41.541 jqb[7537:2410265] app = <LSApplicationProxy: 0x1566a7240> com.apple.videos <file:///Applications/Videos.app>
2016-06-01 11:11:41.541 jqb[7537:2410265] app = <LSApplicationProxy: 0x1566a7800> com.apple.social.SLGoogleAuth <file:///Applications/SLGoogleAuth.app>
2016-06-01 11:11:41.542 jqb[7537:2410265] app = <LSApplicationProxy: 0x1566a7c10> com.apple.mobileslideshow <file:///Applications/MobileSlideShow.app>
2016-06-01 11:11:41.542 jqb[7537:2410265] app = <LSApplicationProxy: 0x15654ed10> com.apple.Health <file:///Applications/Health.app>
2016-06-01 11:11:41.542 jqb[7537:2410265] app = <LSApplicationProxy: 0x15654f240> com.apple.news <file:///Applications/News.app>
2016-06-01 11:11:41.542 jqb[7537:2410265] app = <LSApplicationProxy: 0x15654fa30> com.apple.AccountAuthenticationDialog <file:///Applications/AccountAuthenticationDialog.app>
2016-06-01 11:11:41.542 jqb[7537:2410265] app = <LSApplicationProxy: 0x15654fff0> com.apple.purplebuddy <file:///Applications/Setup.app>
2016-06-01 11:11:41.542 jqb[7537:2410265] app = <LSApplicationProxy: 0x156550450> com.apple.social.SLYahooAuth <file:///Applications/SLYahooAuth.app>
2016-06-01 11:11:41.542 jqb[7537:2410265] app = <LSApplicationProxy: 0x156550930> com.apple.AdSheetPhone <file:///Applications/AdSheet.app>
2016-06-01 11:11:41.542 jqb[7537:2410265] app = <LSApplicationProxy: 0x156550ee0> com.apple.Diagnostics <file:///Applications/Diagnostics.app>
2.判斷手機中是否安裝了某個App
這就須要對咱們獲取到的allApplications作一下處理,這個數組中裝的並非常規的字符串,咱們能夠從打印結果看出,是一個LSApplicationProxy類型的,咱們要把這個轉換成字符串,並且咱們能夠從打印結果看出,這個字符串中不僅包含了App的bundle ID,還包含了安裝的路徑,由於這個路徑不惟一不肯定,咱們判斷是否安裝了某個App只需判斷這個字符串中的bundle ID,全部咱們還要對字符串作處理。
NSInteger zlConnt = 0;
for (NSString *appStr in allApplications) {
NSString *app = [NSStringstringWithFormat:@"%@",appStr];//
轉換成字符串
NSRange range = [app rangeOfString:@"org.ios.appprojuct.jqb」];//是否包含這個
bundle ID
if (range.length > 1) {
zlConnt ++;
}
}
if (zlConnt >= 1) {
NSLog(@"已安裝金錢豹org.ios.appprojuct.jqb"); }