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

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

dlopen 是打开库文件

dlsym 是获取函数地址

dlclose 是关闭。

 

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

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

下面是两个使用的例子

1: 这是使用 coreTelephony.framework 获取 ims i

#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