iphone开发中使用动态库(dylib)和动态加载framework (获取iphone的IMSI和设置飞行模式)

http://blog.sina.com.cn/s/blog_61f4999d0100x8zc.html


iphone上使用动态库的多为dylib文件,这些文件使用标准的dlopen方式来使用是可以的。那相同的在使用framework文件也可以当做动态库的方式来动态加载,这样就可以比较自由的使用apple私有的framework了。

dlopen是打开库文件

dlsym是获取函数地址

dlclose是关闭。

 

当然,要使用这种方式也是有明显缺陷的,那就是你要知道函数名和参数,否则无法继续。

私有库的头文件可以使用class dump的方式导出来,这个详细的就需要google了。

下面是两个使用的例子

1: 这是使用coreTelephony.framework获取imsi

#define PRIVATE_PATH  "/System/Library/PrivateFrameworks/CoreTelephony.framework/CoreTelephony"

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
#if !TARGET_IPHONE_SIMULATOR
    void *kit = dlopen(PRIVATE_PATH,RTLD_LAZY);    
    NSString *imsi = nil;
    int (*CTSIMSupportCopyMobileSubscriberIdentity)() = dlsym(kit, "CTSIMSupportCopyMobileSubscriberIdentity");
    imsi = (NSString*)CTSIMSupportCopyMobileSubscriberIdentity(nil);
    dlclose(kit);    

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"IMSI" 
                                                    message:imsi 
                                                   delegate:self 
                                          cancelButtonTitle:@"OK" 
                                          otherButtonTitles:nil];
    [alert show];
    [alert release];
#endif
}


 

2:这是使用SpringBoardServices.framework来设置飞行模式开关

#ifdef SUPPORTS_UNDOCUMENTED_API
#define SBSERVPATH  "/System/Library/PrivateFrameworks/SpringBoardServices.framework/SpringBoardServices"
#define UIKITPATH "/System/Library/Framework/UIKit.framework/UIKit"

// Don't use this code in real life, boys and girls. It is not App Store friendly.
// It is, however, really nice for testing callbacks
+ (void) setAirplaneMode: (BOOL)status;
{
    mach_port_t *thePort;
    void *uikit = dlopen(UIKITPATH, RTLD_LAZY);
    int (*SBSSpringBoardServerPort)() = dlsym(uikit, "SBSSpringBoardServerPort");
    thePort = (mach_port_t *)SBSSpringBoardServerPort(); 
    dlclose(uikit);
    
    // Link to SBSetAirplaneModeEnabled
    void *sbserv = dlopen(SBSERVPATH, RTLD_LAZY);
    int (*setAPMode)(mach_port_t* port, BOOL status) = dlsym(sbserv, "SBSetAirplaneModeEnabled");
    setAPMode(thePort, status);
    dlclose(sbserv);
}
#endif


你可能感兴趣的:(iphone开发中使用动态库(dylib)和动态加载framework (获取iphone的IMSI和设置飞行模式))