iphone开发中使用动态库(dylib)和动态加载framework(iphone获取IMSI和设置飞行模式)
2011-05-30 23:44
351 查看
在 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
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
相关文章推荐
- iphone开发中使用动态库(dylib)和动态加载framework (获取iphone的IMSI和设置飞行模式)
- iphone开发中使用动态库(dylib)和动态加载framework (获取iphone的IMSI和设置飞行模式)
- iphone开发中使用动态库(dylib)和动态加载framework (获取iphone的IMSI和设置飞行模式)
- iphone开发中使用动态库(dylib)和动态加载framework (获取iphone的IMSI和设置飞行模式)
- iphone开发中使用动态库(dylib)和动态加载framework (获取iphone的IMSI和设置飞行模式) .
- iphone开发中使用动态库(dylib)和动态加载framework
- iOS 使用动态库(dylib)和动态加载framework
- iOS 使用动态库(dylib)和动态加载framework
- iOS 使用动态库(dylib)和动态加载framework
- iOS 使用动态库(dylib)和动态加载framework
- 获取iphone的IMSI和设置飞行模式
- iphone开发中使用动态库(dylib)和…
- 【linux 开发】Linux 加载使用动态库实例
- Android--使用动态加载框架DL进行插件化开发
- Asp.net开发心得点滴[动态加载的用户控件使用事件委托,交给页面处理的事件无效问题]
- Style样式的四种使用(包括用C#代码动态加载资源文件并设置样式)
- Android 使用动态加载框架DL进行插件化开发
- 【Android开发学习44】android: 静态XML和动态加载XML混合使用,以及重写Layout控件
- Android 使用动态加载框架DL进行插件化开发
- Unity3D游戏开发之使用AssetBundle和Xml实现场景的动态加载