objc/runtime.h 相關


Objecitve-C的重要特性是Runtime(運行時),在Interacting with the Runtime(交互運行)中,運行時函數部分,蘋果給出了/usr/lib/libobjc.A.dylib庫,這個共享庫提供支持動態屬性的objective - c語言,通過其接口,可以用於開發將其他語言運行於Objective-C上的中間層(橋接層),庫里的函數定義為純C語言。

例如:class_getName

 

  1. class_getName  
  2. Returns the name of a class.  
  3.   
  4. const char * class_getName(Class cls)  
  5. Parameters  
  6. cls  
  7. class object.  
  8. Return Value  
  9. The name of the class, or the empty string if cls is Nil.  
  10.   
  11. Declared In  
  12. runtime.h  



 

這里我們要用庫里的函數,干個“壞事”,查看蘋果SDK的私有方法。

第一步:導入頭文件

  1. #import <objc/runtime.h>   

第二步:添加下列代碼

 

 **************實用************

  1. NSString *className = NSStringFromClass([UIView class]);  
  2.   
  3.       
  4.     const char *cClassName = [className UTF8String];  
  5.       
  6.     id theClass = objc_getClass(cClassName);  
  7.       
  8.     unsigned int outCount;  
  9.       
  10.   
  11.     Method *m =  class_copyMethodList(theClass,&outCount);  
  12.       
  13.     NSLog(@"%d",outCount);  
  14.     for (int i = 0; i<outCount; i++) {  
  15.         SEL a = method_getName(*(m+i));  
  16.         NSString *sn = NSStringFromSelector(a);  
  17.         NSLog(@"%@",sn);  
  18.     }   

第三步:想看什么類 就把UIView換成你想要的

 

當然,如果只是查看私有API,會有更簡單的方法,可以使用工具class-dump,也可以通過此鏈接,https://github.com/kennytm/iphone-private-frameworks/tree/master/UIKit/    查看。

特別注意的是,如果是需要上架的APP,切勿使用私有API,會通不過APP Store的審核。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM