美文网首页
获取当前工程Mach-O的方法名类名

获取当前工程Mach-O的方法名类名

作者: 西博尔 | 来源:发表于2016-12-29 14:07 被阅读301次

    1.类名

    unsigned long size;
        
            uint8_t *ptrClassName = getsectiondata(&_mh_execute_header, "__TEXT", "__objc_classname", &size);
        
            NSData *dataClassName = [NSData dataWithBytesNoCopy:ptrClassName length:size freeWhenDone:NO];
        
            NSString * strClassName = [[NSString alloc]initWithData:dataClassName encoding:NSUTF8StringEncoding];
        
            NSArray * arrayClassName = [strClassName componentsSeparatedByString:@"\00"];
        
            [arrayClassName enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
                NSLog(@"className = %@  interger = %lu",obj, (unsigned long)idx);
        
            }];
    

    2.方法名

    unsigned long sizeMethName;
        
            uint8_t * ptrMethName = getsectiondata(&_mh_execute_header, "__TEXT", "__objc_methname", &sizeMethName);
    
            NSData * dataMethName = [NSData dataWithBytesNoCopy:ptrMethName length:sizeMethName freeWhenDone:NO];
        
            NSString * strMethName = [[NSString alloc]initWithData:dataMethName encoding:NSUTF8StringEncoding];
    
            NSArray * arrMethName = [strMethName componentsSeparatedByString:@"\00"];
        
            [arrMethName enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
                NSLog(@"methName = %@",obj);
        
            }];
    
    3.可能遇到的问题

    方法名或者类名遇到特殊字符, data->string的时候闪退, 那就把NSData->NSString的方法换成下面的

     NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding ( kCFStringEncodingGB_18030_2000 );
    
        NSString* strMethName = [[NSString alloc] initWithData:dataMethName encoding:enc];
    
    

    相关文章

      网友评论

          本文标题:获取当前工程Mach-O的方法名类名

          本文链接:https://www.haomeiwen.com/subject/gitpvttx.html