在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 SprintBoard部分私有API总结

本文介绍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的更多相关文章

  1. windows动态库与Linux动态库

    Linux动态库和windows动态库的目的是基本一致的,但由于操作系统的不同,他们在许多方面还是不尽相同.但是尽管有差异Linux动态库的windows动态库还是可以移植的,有一些规则以及经验是必须 ...

  2. js动态创建的select2标签样式加载不上解决办法

    js动态创建的select2标签样式加载不上:调用select2的select2()函数来初始化一下: js抛出了Uncaught query function not defined for Sel ...

  3. [WPF自定义控件库] 让Form在加载后自动获得焦点

    原文:[WPF自定义控件库] 让Form在加载后自动获得焦点 1. 需求 加载后让第一个输入框或者焦点是个很基本的功能,典型的如"登录"对话框.一般来说"登录" ...

  4. IOS 开发下拉刷新和上拉加载更多

    IOS 开发下拉刷新和上拉加载更多 简介 1.常用的下拉刷新的实现方式 (1)UIRefreshControl (2)EGOTTableViewrefresh (3)AH3DPullRefresh ( ...

  5. 第一百五十七节,封装库--JavaScript,预加载图片

    封装库--JavaScript,预加载图片 首先了解一个Image对象,为图片对象 Image对象 var temp_img = new Image();   //创建一个临时区域的图片对象alert ...

  6. macOS下加载动态库dylib报"code signature invalid"错误的解决办法

    一.现象描述 在macOS上搞开发也有一段时间了,也积攒了一定的经验.然而,今天在替换工程中的一个动态库时还是碰到了一个问题.原来工程中用的是一个静态库,调试时发现有问题就把它替换成了动态库.这本来没 ...

  7. 热更新--动态加载framework

    1.准备工作:先自己封装一个framework:http://www.cnblogs.com/sunjianfei/p/5781863.html 2.把封装好的framework压缩成zip,放到本地 ...

  8. C#调用C++动态库方法及动态库封装总结

    我只是粗浅的学习过一些C++语法, 变量类型等基础内容, 如有不对的地方还望指出. 如果你跟我一样, 对指针操作不了解, 对封装C++动态库头疼的话, 下面内容还是有帮助的. 转载请注明出处: htt ...

  9. java动态编译类文件并加载到内存中

    如果你想在动态编译并加载了class后,能够用hibernate的数据访问接口以面向对象的方式来操作该class类,请参考这篇博文-http://www.cnblogs.com/anai/p/4270 ...

随机推荐

  1. zookeeper(1)

    参考文档:zookeeper中文网 一.介绍安装 zookeeper 是一个高效的分布式协调服务,它暴露了一些公用服务,比如命名/配置/同步控制/群组服务等.我们可以使用ZK来实现一些功能,例如:达成 ...

  2. webapi中的自定义路由约束

    Custom Route Constraints You can create custom route constraints by implementing the IHttpRouteConst ...

  3. [ An Ac a Day ^_^ ] CodeForces 601A The Two Routes 最短路

    14号就ccpc全国赛的全国赛了 而且也快东北赛的选拔赛了 现在队伍实力实在不行 参加了也是边缘化的队伍 虽然有新生保护的设置 但实话说 机会还是不大 所以不如趁现在开始好好努力 明年也许还有机会 A ...

  4. FZU 1896 神奇的魔法数 dp

    网上都说是数位dp 但是虽然在队伍里负责动态规划 但是数位dp还不会…… 百度了一下 发现和最大子序列思路差不多…… 最大子序列的dp[i][j]是表示两个序列前i项和前j项的最大子序列…… dp[i ...

  5. BASE2(matlab)

    %{ // %} clc % linspace(3,5) 3到5 分成100 default %{ a=1 b=2 str = [num2str(a),'+',num2str(b)] eval(str ...

  6. xml 和json 数据格式及解析

    来源:http://blog.jobbole.com/79252/ 引言 NOKIA 有句著名的广告语:“科技以人为本”.任何技术都是为了满足人的生产生活需要而产生的.具体到小小的一个手机,里面蕴含的 ...

  7. Html的Table与Echart的饼图实现联动效果

    功能描述: 单击Table中的某个单元格,Echart的饼图加载相关的数据,鼠标悬停在Echarts饼图中的某一块中,Table显示与Echarts饼图相关的数据. 例:楼宇经济概要显示每一个季度所有 ...

  8. py执行系统命令

    1. os.system In [32]: run = os.system("date") Thu Jan 28 09:41:25 CST 2016 In [33]: run Ou ...

  9. SharedPreferences的工具类

    import android.content.Context; import android.content.SharedPreferences; import android.content.Sha ...

  10. HDU 3410 Passing the Message

    可以先处理出每个a[i]最左和最右能到达的位置,L[i],和R[i].然后就只要询问区间[ L[i],i-1 ]和区间[ i+1,R[i] ]最大值位置即可. #include<cstdio&g ...