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

本文介绍如何在iPhone上利用动态库加载私有framework的方法,包括使用dlopen、dlsym及dlclose等函数实现对CoreTelephony.framework和SpringBoardServices.framework的调用,以获取IMSI号码和设置飞行模式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值