獲取iOS應用資訊
阿新 • • 發佈:2019-01-25
由於蘋果的限制,在未越獄的 iOS 裝置中只能通過私有 api 來獲取安裝應用列表和所有的 url scheme,在 ios 中可以用獲取到的 scheme 來開啟對應 app
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@“weixin”]];
- 1
- 1
也可以獲取 bundle id 後通過私有 api 來完成。
1、 iOS 私有 api 的使用
注意,並不是每個 api 都可以使用,蘋果的限制是越來越嚴的。
然後便是私有 api 的使用,這裡介紹一種方法,比如我要使用 LSApplicationWorkspace 中的 allInstalledApplications 方法來獲取所有安裝程式,首先申明
@interface PrivateApi_LSApplicationWorkspace
- (NSArray*)allInstalledApplications;
@end
- 1
- 2
- 3
- 1
- 2
- 3
學過c的應該能很好的理解,接著便是在某個地方進行初使化
PrivateApi_LSApplicationWorkspace* _workspace;
_workspace = [NSClassFromString(@"LSApplicationWorkspace") new];
- 1
- 2
- 1
- 2
然後便可以呼叫了
NSArray* allInstalledApplications = [_workspace allInstalledApplications];
- 1
- 1
2、獲取所有安裝程式極其資訊
@interface PrivateApi_LSApplicationProxy
+ (instancetype)applicationProxyForIdentifier:(NSString*)identifier;
@property (nonatomic, readonly) NSString* localizedShortName;
@property (nonatomic, readonly) NSString* localizedName;
@property (nonatomic, readonly) NSString* bundleIdentifier;
@property (nonatomic, readonly) NSArray* appTags;
@property (nonatomic, readonly) NSString *applicationDSID;
@property (nonatomic, readonly) NSString *applicationIdentifier;
@property (nonatomic, readonly) NSString *applicationType;
@property (nonatomic, readonly) NSNumber *dynamicDiskUsage;
@property (nonatomic, readonly) NSArray *groupIdentifiers;
@property (nonatomic, readonly) NSNumber *itemID;
@property (nonatomic, readonly) NSString *itemName;
@property (nonatomic, readonly) NSString *minimumSystemVersion;
@property (nonatomic, readonly) NSArray *requiredDeviceCapabilities;
@property (nonatomic, readonly) NSString *roleIdentifier;
@property (nonatomic, readonly) NSString *sdkVersion;
@property (nonatomic, readonly) NSString *shortVersionString;
@property (nonatomic, readonly) NSString *sourceAppIdentifier;
@property (nonatomic, readonly) NSNumber *staticDiskUsage;
@property (nonatomic, readonly) NSString *teamID;
@property (nonatomic, readonly) NSString *vendorName;
@end
@implementation LMApp
- (BOOL)isHiddenApp
{
return [[_applicationProxy appTags] indexOfObject:@"hidden"] != NSNotFound;
}
- (id)initWithPrivateProxy:(id)privateProxy
{
self = [super init];
if(self != nil)
{
_applicationProxy = (PrivateApi_LSApplicationProxy*)privateProxy;
}
return self;
}
- (instancetype)initWithBundleIdentifier:(NSString*)bundleIdentifier
{
self = [super init];
if(self != nil)
{
_applicationProxy = [NSClassFromString(@"LSApplicationProxy") applicationProxyForIdentifier:bundleIdentifier];
}
return self;
}
+ (instancetype)appWithPrivateProxy:(id)privateProxy
{
return [[self alloc] initWithPrivateProxy:privateProxy];
}
+ (instancetype)appWithBundleIdentifier:(NSString*)bundleIdentifier
{
return [[self alloc] initWithBundleIdentifier:bundleIdentifier];
}
@end
NSArray* allInstalledApplications = [_workspace allInstalledApplications];
NSMutableArray* applications = [NSMutableArray arrayWithCapacity:allInstalledApplications.count];
for(id proxy in allInstalledApplications)
{
LMApp* app = [LMApp appWithPrivateProxy:proxy];
if(!app.isHiddenApp)
{
[applications addObject:app];
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
於是 applications 便是所有的安裝程式,再訪問其成員便可知道各應用的額外資訊,比如 bundle id 什麼的,這裡還要特別說明下獲取應用圖示的方法
@interface UIImage ()
+ (id)_iconForResourceProxy:(id)arg1 variant:(int)arg2 variantsScale:(float)arg3;
+ (id)_applicationIconImageForBundleIdentifier:(id)arg1 format:(int)arg2 scale:(double)arg3;
@end
UIImage* icon = [UIImage _applicationIconImageForBundleIdentifier:@"com.tencent.xin" format:10 scale:2.0]; //微信圖示
- 1
- 2
- 3
- 4
- 5
- 6
- 1
- 2
- 3
- 4
- 5
- 6
3、開啟對應bundle id 的應用
與上面類似,呼叫相應私有 api 便可
[_workspace openApplicationWithBundleID:@"com.tencent.xin"]; // 開啟微信
- 1
- 1
4、檢視所有 url schemes
出於某些原因,可能我們想要知道某些 app 有 url scheme,在這裡只能列出所有的 url scheme 然後猜測是哪個應用的,或者可以寫個程式配置好相應 info.plist 直接在程式中用 openURL 方式開啟對應 應用
- (NSArray*)privateURLSchemes
{
return [_workspace privateURLSchemes];
}
- (NSArray*)publicURLSchemes
{
return [_workspace publicURLSchemes];
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9