IOS 使用动态库(dylib)和动态加载framework
在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
本文介绍iOS SrpintBoard框架的部分私有API,具体包括:
- 获取ios上当前正在运行的所有App的bundle id(不管当前程序是在前台还是后台都可以)
- 获取ios上当前前台运行的App的bundle id(不管当前程序是在前台还是后台都可以)
- 根据ios app的bundle id得到其App名称、图标(不管当前程序是在前台还是后台都可以)
- 直接通过App 的bundle id来运行该App,无需使用url scheme(仅限当前程序在前台时,假如程序在后台能随便运行其他App,那就无敌了@_@)
(1)初始化
void * uikit = dlopen("/System/Library/Framework/UIKit.framework/UIKit", RTLD_LAZY);
int (*SBSSpringBoardServerPort)() =
dlsym(uikit, "SBSSpringBoardServerPort");
p = (mach_port_t *)SBSSpringBoardServerPort();
dlclose(uikit);
sbserv = dlopen(SBSERVPATH, RTLD_LAZY);
(2)获取iphone上所有正在运行的app的bundle id列表
NSArray* (*SBSCopyApplicationDisplayIdentifiers)(mach_port_t* port, BOOL runningApps,BOOL debuggablet) =
dlsym(sbserv, "SBSCopyApplicationDisplayIdentifiers");
NSArray *currentRunningAppBundleIdArray= SBSCopyApplicationDisplayIdentifiers(p,NO,YES);
(3)得到iphone 前台运行的app的bundle id
void* (*SBFrontmostApplicationDisplayIdentifier)(mach_port_t* port,char * result) = dlsym(sbserv, "SBFrontmostApplicationDisplayIdentifier");
char topapp[256];
SBFrontmostApplicationDisplayIdentifier(p,topapp);
currentTopAppBundleId=[NSStringstringWithFormat:@"%s",topapp];
(4)根据iphone app的bundle id得到其app名称
NSString * (*SBSCopyLocalizedApplicationNameForDisplayIdentifier)(NSString* ) = dlsym(sbserv, "SBSCopyLocalizedApplicationNameForDisplayIdentifier");
NSString *strAppName = SBSCopyLocalizedApplicationNameForDisplayIdentifier(strBundleId);
(5)根据iphone app 的bundle id得到其图标
NSData* (*SBSCopyIconImagePNGDataForDisplayIdentifier)(NSString * bundleid) =
dlsym(sbserv, "SBSCopyIconImagePNGDataForDisplayIdentifier");
UIImage *icon = nil;
NSData *iconData = SBSCopyIconImagePNGDataForDisplayIdentifier(bundleid);
if (iconData != nil) {
icon = [UIImage imageWithData:iconData];
}
return icon;
(6)直接通过app 的bundle id来运行该app
在ios中,一个app调起另一个app的方式通常是用url scheme,但是用这个 私有app,可以在不需要url scheme的情况下运行任何app
-(void)openAppByBundleId:(NSString*)bundleId
{
void* sbServices = dlopen("/System/Library/PrivateFrameworks/SpringBoardServices.framework/SpringBoardServices", RTLD_LAZY);
int (*SBSLaunchApplicationWithIdentifier)(CFStringRef identifier, Boolean suspended) = dlsym(sbServices, "SBSLaunchApplicationWithIdentifier");
const char *strBundleId = [bundleId cStringUsingEncoding:NSUTF8StringEncoding];
int result = SBSLaunchApplicationWithIdentifier((__bridge CFStringRef)bundleId, NO);
dlclose(sbServices);
}
IOS 使用动态库(dylib)和动态加载framework的更多相关文章
- windows动态库与Linux动态库
Linux动态库和windows动态库的目的是基本一致的,但由于操作系统的不同,他们在许多方面还是不尽相同.但是尽管有差异Linux动态库的windows动态库还是可以移植的,有一些规则以及经验是必须 ...
- js动态创建的select2标签样式加载不上解决办法
js动态创建的select2标签样式加载不上:调用select2的select2()函数来初始化一下: js抛出了Uncaught query function not defined for Sel ...
- [WPF自定义控件库] 让Form在加载后自动获得焦点
原文:[WPF自定义控件库] 让Form在加载后自动获得焦点 1. 需求 加载后让第一个输入框或者焦点是个很基本的功能,典型的如"登录"对话框.一般来说"登录" ...
- IOS 开发下拉刷新和上拉加载更多
IOS 开发下拉刷新和上拉加载更多 简介 1.常用的下拉刷新的实现方式 (1)UIRefreshControl (2)EGOTTableViewrefresh (3)AH3DPullRefresh ( ...
- 第一百五十七节,封装库--JavaScript,预加载图片
封装库--JavaScript,预加载图片 首先了解一个Image对象,为图片对象 Image对象 var temp_img = new Image(); //创建一个临时区域的图片对象alert ...
- macOS下加载动态库dylib报"code signature invalid"错误的解决办法
一.现象描述 在macOS上搞开发也有一段时间了,也积攒了一定的经验.然而,今天在替换工程中的一个动态库时还是碰到了一个问题.原来工程中用的是一个静态库,调试时发现有问题就把它替换成了动态库.这本来没 ...
- 热更新--动态加载framework
1.准备工作:先自己封装一个framework:http://www.cnblogs.com/sunjianfei/p/5781863.html 2.把封装好的framework压缩成zip,放到本地 ...
- C#调用C++动态库方法及动态库封装总结
我只是粗浅的学习过一些C++语法, 变量类型等基础内容, 如有不对的地方还望指出. 如果你跟我一样, 对指针操作不了解, 对封装C++动态库头疼的话, 下面内容还是有帮助的. 转载请注明出处: htt ...
- java动态编译类文件并加载到内存中
如果你想在动态编译并加载了class后,能够用hibernate的数据访问接口以面向对象的方式来操作该class类,请参考这篇博文-http://www.cnblogs.com/anai/p/4270 ...
随机推荐
- iOS开发的一些奇巧淫技(转载)
iOS开发的一些奇巧淫技 http://www.cocoachina.com/ios/20141229/10783.html iOS开发的一些奇巧淫技2 http://www.cocoachina.c ...
- ubuntu下安装UltraEdit
在windows下常年使用UltraEdit来查看log,现在突然切换到ubuntu下,系统自带的Text Editor相当不适应:只有自己安装了. 首先,需要下载安装包,可以去:http://www ...
- 虚拟机 centos 7 nginx安装
1下载vmware 12,并安装.百度即可 2下载centos 7,将其安装在vmware 12中.百度即可,无复杂设置. 3设置vmware 中centos7能上网: a.右键计算机->管理- ...
- sql语句的使用;
1.导出数据库的语句: mysqldump -u root -p shop > d:\shop.sql
- ubuntu 下配置vim for python
apt-get install vim-gnome apt-get install ctags apt-get install vim-scripts vim-addons install tagli ...
- jquery的隐藏
HTML 代码:<form> <input type="text" name="email" /> <input type=&qu ...
- freemarker遍历list中的map
前台: <select id="jq" name="jq" class="tsui" data-required="true ...
- test错误记录
1.Caused by: java.io.FileNotFoundException: D:\Program%20Files\Apache%20Software%20Foundation\Tomcat ...
- Python中pip安装问题解决
用国内镜像通过pip安装python的一些包,有时会出现安装失败, 为什么总是失败?自己操作老标准了,这么简单的几个小步骤还老是出错,不由得让我怀疑是否是撞墙了,可是又懒得买vpn去翻~~一墙,无法代 ...
- HTTP 返回状态值详解
当用户点击或搜索引擎向网站服务器发出浏览请求时,服务器将返回Http Header Http头信息状态码,常见几种如下: 1.Http/1.1 200 OK 访问正常 表示成功访问,为网站可正常访问 ...