判断App是否安装
一、需求判断游戏是否已经安装
iOS新版本的系统上苹果已经不允许通过scheme判断app是否安装,可以通过以下手段判断一个app是否安装
二、下面的方法调用的私有api,如果需要提交appstore使用的话,需要对selector进行混淆
使用过程中需要注意线程安全问题
// // LAApplicationHelper.h // App // // Created by qqvipfunction on 2017/4/1. // Copyright © 2017年 SZ. All rights reserved. // #import <Foundation/Foundation.h> @interface LAApplicationHelper : NSObject + (instancetype)shareInstance; - (void)refreshInstalledList; - (BOOL)isAppInstalled:(NSString *)appBundleID; - (NSArray<NSString *> *)appList; @end
实现文件
// // LAApplicationHelper.m // App // // Created by qqvipfunction on 2017/4/1. // Copyright © 2017年 SZ. All rights reserved. // #import "LAApplicationHelper.h" #import <objc/runtime.h> @interface LAApplicationHelper () @property (nonatomic, strong) NSMutableArray* appList; @property (nonatomic, strong) NSMutableArray* appNameList; @end @implementation LAApplicationHelper + (instancetype)shareInstance { static dispatch_once_t onceToken; static LAApplicationHelper *instance = nil; dispatch_once(&onceToken, ^{ instance = [[LAApplicationHelper alloc] init]; }); return instance; } - (instancetype)init { if(self = [super init]) { _appList = [[NSMutableArray alloc] init]; _appNameList = [[NSMutableArray alloc] init]; [self loadInstalledApps]; } return self; } - (void)refreshInstalledList { [self loadInstalledApps]; } - (void)loadInstalledApps { #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wundeclared-selector" [_appList removeAllObjects]; [_appNameList removeAllObjects]; Class LSApplicationWorkspace_class = objc_getClass("LSApplicationWorkspace"); NSObject* workspace = [LSApplicationWorkspace_class performSelector:@selector(defaultWorkspace)]; NSArray *arr = [workspace performSelector:@selector(allApplications)]; if([arr count] > 0) { for (NSObject * obj in arr) { [_appList addObject:obj]; NSArray *itemArr = [[obj description] componentsSeparatedByString:@" "]; if([itemArr count] > 2) { NSString *appName = itemArr[2]; if([appName length] > 0) { [_appNameList addObject:appName]; } } } } #pragma clang diagnostic pop } - (BOOL)isAppInstalled:(NSString *)appBundleID { for (NSString *bundleID in self.appNameList) { if([appBundleID isEqual:bundleID]) return YES; } return NO; } - (NSArray<NSString *> *)appList { return [self.appNameList copy]; } @end