判断手机中是否安装了某个App

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");
       
    }
   
posted @ 2016-06-01 14:34  彩虹丶直至黑白  阅读(1543)  评论(0编辑  收藏  举报