1. 程式人生 > >iOS-私有API與runtime

iOS-私有API與runtime

有朋友在做類似iTool的功能,跟我聊起來,這幾天閒,就寫了一個demo,不是正經做這個,還很粗略,具體乾貨諸位等這位朋友自己發文吧。

DEMO

思路

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列表

雖然沒有註釋,但是我們可以猜到這個方法應該可以得到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
著作權歸作者所有,轉載請聯絡作者獲得授權,並標註“簡書作者”。