標籤:
轉載:http://www.jianshu.com/p/6167b9ce7af8序
有朋友在做類似iTool的功能,跟我聊起來,這幾天閑,就寫了一個demo,不是正經做這個,還很粗略,具體乾貨諸位等這位朋友自己發文吧。
DEMO
https://github.com/liulishuo/testAppList
思路
iOS9白名單的上限是50個,如果想繞過這個限制,掃描系統中所有app的狀態,只有使用私人API,需要用到的類有兩個:LSApplicationWorkspace、LSApplicationProxy,知道類的名字我們就可以依靠runtime得到這個類,以及這個類的所有方法,OC的方法望文生義,接下來就可以慢慢嘗試。
實現
- 得到LSApplicationWorkspace、LSApplicationProxy
Class LSApplicationWorkspace_class = objc_getClass("LSApplicationWorkspace");Class LSApplicationProxy_class = object_getClass(@"LSApplicationProxy");
-
得到類的所有方法與成員變數(編程小翁)
//擷取不到成員變數 int count = 0; Ivar *members = class_copyIvarList([LSApplicationProxy_class class], &count); for (int i = 0 ; i < count; i++) { Ivar var = members[i]; const char *memberName = ivar_getName(var); const char *memberType = ivar_getTypeEncoding(var); NSLog(@"%s: %s",memberType,memberName); } NSLog(@"count: %d",count); //擷取不到有用的方法 count = 0; Method *memberMethods = class_copyMethodList(LSApplicationProxy_class, &count); for (int i = 0; i < count; i++) { SEL name = method_getName(memberMethods[i]); NSString *methodName = [NSString stringWithCString:sel_getName(name) encoding:NSUTF8StringEncoding]; NSLog(@"member method:%@", methodName); } NSLog(@"count: %d",count);
因為函數class_copyIvarList、class_copyMethodList有時不能返回有用的結果,所以我們使用class-dump(有朋友反映xcode7的庫導不出來,大家用源碼自己build一個吧),匯出類的標頭檔。
匯出MobileCoreServices.framework的所有標頭檔:
class-dump -H /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/System/Library/Frameworks/MobileCoreServices.framework -o /Users/credit/Desktop/header004
-o後面是輸出路徑 改成你需要的。
MobileCoreServices.framework的所有標頭檔
LSApplicationProxy
LSApplicationWorkspace
雖然沒有注釋,但是我們可以猜到這個方法應該可以得到app列表
- (id)allApplications;
但是他是執行個體方法,我們先要拿到一個LSApplicationWorkspace執行個體
+ (id)defaultWorkspace;
代碼如下
NSObject* workspace = [LSApplicationWorkspace_class performSelector:@selector(defaultWorkspace)];NSArray *appList = [workspace performSelector:@selector(allApplications)];
- 遍曆app列表
applist裡的每一個元素都是LSApplicationProxy 比對其標頭檔,把對應的屬性列印出來研究,
屬性略多,不想自己寫的朋友,請看我的demo
遍曆數組
YY
末
注意groupContainers數組的內容,我們可以拿到group id,可不可以拿到公用儲存區的資料呢?
NSUserDefaults *share = [[NSUserDefaults alloc] initWithSuiteName:@"group.com.tencent.xin"]; NSLog(@"%@",share.dictionaryRepresentation);
然而並沒有,那有沒有共用資料的目錄呢?
NSURL *containerURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.com.tencent.xin"];
傳回值為nil
app不在group.com.tencent.xin這個組內,咱的冒牌貨強寫是無效的,因為這個標識符已經被正品佔用了。
App Groups
還有什麼好玩的? 試試這個
[workspace performSelector:@selector(uninstallApplication:withOptions:) withObject:@"XXX" withObject:nil];
workspace 是LSApplicationWorkspace執行個體,@"XXX"這裡填你擷取到的applicationIdentifier
模擬器可以正常卸載app,真機不行。
更多API,大家可以自己嘗試一下。
文/cocoa(簡書作者)
原文連結:http://www.jianshu.com/p/6167b9ce7af8
著作權歸作者所有,轉載請聯絡作者獲得授權,並標註“簡書作者”。
iOS-私人API與runtime