0. 参考

  http://www.cocoachina.com/industry/20140613/8810.html

  framework+xib参考 : http://blog.csdn.net/xyxjn/article/details/42527341

1. 设置主头文件

  系统已经自动生成好了。

2. 设置公开的头文件

  工程导航栏>"工程名">Build Phases>菜单栏Editor>Add Build Phases>Add Headers Build Phase。

  如果该项不能选择,则点击下方的Build Phases的区域获取焦点。其它需要公开的头文件就放到Public下面。

3. 编译后就生成了framework

4. 合并模拟器framework和真机framework

  iOS默认生成两个framework,一个用于模拟器,一个用于真机。这样的话,用起来模拟器和真机可能会表现不一定。所以,合并成通用的framework是很好的方案。并且这是可行的。合并完了之后呢,顺便要把生成的framework放到工程的制定Products目录下。因为Products名称是生成的默认目录名称。

  怎么进行合并呢?

4.1. 创建Aggregate Target

4.1.0. 参考

iOS 之 Aggregate Target,在这里我取名为:aggregateTest

4.1.1. 设置Target Dependencies

  TARGETS-->选中“aggregateTest”-->Build Phases-->Target Dependencies  ,将真正的动态库添加到其中。

4.2. 脚本

4.2.1. 目的

  • 分别编译生成真机和模拟器使用的framework;
  • 使用lipo命令将其合并成一个通用framework;
  • 最后将生成的通用framework放置在工程根目录下新建的Products目录下。

4.2.2. 路径

  TARGETS-->选中“aggregateTest”-->Build Phases-->Run Script-->左上角+-->New Run Script Phase

4.2.3. 内容

# Sets the target folders and the final framework product.
FMK_NAME=${PROJECT_NAME} # Install dir will be the final output to the framework.
# The following line create it in the root folder of the current project.
INSTALL_DIR=${SRCROOT}/Products/${FMK_NAME}.framework # Working dir will be deleted after the framework creation.
WRK_DIR=build
DEVICE_DIR=${WRK_DIR}/Release-iphoneos/${FMK_NAME}.framework
SIMULATOR_DIR=${WRK_DIR}/Release-iphonesimulator/${FMK_NAME}.framework # -configuration ${CONFIGURATION}
# Clean and Building both architectures.
xcodebuild -configuration "Release" -target "${FMK_NAME}" -sdk iphoneos clean build
xcodebuild -configuration "Release" -target "${FMK_NAME}" -sdk iphonesimulator clean build # Cleaning the oldest.
if [ -d "${INSTALL_DIR}" ]
then
rm -rf "${INSTALL_DIR}"
fi mkdir -p "${INSTALL_DIR}" cp -R "${DEVICE_DIR}/" "${INSTALL_DIR}/" # Uses the Lipo Tool to merge both binary files (i386 + armv6/armv7) into one Universal final product.
lipo -create "${DEVICE_DIR}/${FMK_NAME}" "${SIMULATOR_DIR}/${FMK_NAME}" -output "${INSTALL_DIR}/${FMK_NAME}" rm -r "${WRK_DIR}" open "${SRCROOT}/Products/"

4.2.4. 生成

  在Xcode的左上角选中刚生成的Aggregate>Generic iOS Device ,编译。如果,编译很快,则表示没有成功,因为输入的脚本没有执行。则切换文件失去焦点,让脚本有效,或者重新打开工程。

4.3. 使用动态库

4.3.1. 添加动态库到工程

4.3.1.1. Link Binary with Libraries

  项目导航栏-->选中项目-->Targets-->选中项目-->Build Phases-->Link Binary with Libraries

4.3.1.2. Copy Bundle Resources

  项目导航栏-->选中项目-->Targets-->选中项目-->Build Phases-->Copy Bundle Resources

  如果还不行,就需要:

  项目导航栏-->选中项目-->Targets-->选中项目-->General --> embeded Binaries

4.3.1.3. 为动态库添加链接依赖
4.3.1.3.1. 自动链接依赖

  启动时自动链接:项目导航栏-->选中项目-->Targets-->选中项目-->Build Setting-->Runpath Search Paths,添加@executable_path/

  由于Copy Bundle Resources有动态库,即在main bundle(即沙盒中的.app目录)里面有动态库,所以,添加@executable_path/,表示可执行文件所在目录。

4.3.1.3.1. 需要时链接依赖

  即插即用,需要时再加载动态库。如果是这种方式,那么需要把Targets-->Build Phases-->Link Binary With Libraries中的动态库的Status由Required设置为Optional,或者直接删除也行。

4.3.1.4. 加载动态库
4.3.1.4.1. dlopen 加载
- (IBAction)onDlopenLoadAtPathAction1:(id)sender
{
NSString *documentsPath = [NSString stringWithFormat:@"%@/Documents/xxx.framework/Dylib",NSHomeDirectory()];
[self dlopenLoadDylibWithPath:documentsPath];
} - (void)dlopenLoadDylibWithPath:(NSString *)path
{
libHandle = NULL;
libHandle = dlopen([path cStringUsingEncoding:NSUTF8StringEncoding], RTLD_NOW);
if (libHandle == NULL) {
char *error = dlerror();
NSLog(@"dlopen error: %s", error);
} else {
NSLog(@"dlopen load framework success.");
}
}

  该方式不知道会否通过苹果审核,建议不要使用。

4.3.1.4.1. NSBundle 加载
- (IBAction)onBundleLoadAtPathAction1:(id)sender
{
NSString *documentsPath = [NSString stringWithFormat:@"%@/Documents/Dylib.framework",NSHomeDirectory()];
[self bundleLoadDylibWithPath:documentsPath];
} - (void)bundleLoadDylibWithPath:(NSString *)path
{
_libPath = path;
NSError *err = nil;
NSBundle *bundle = [NSBundle bundleWithPath:path];
if ([bundle loadAndReturnError:&err]) {
NSLog(@"bundle load framework success.");
} else {
NSLog(@"bundle load framework err:%@",err);
}
}
4.3.1.4. 使用动态库的功能

  加载完成后声明类就可以使用了,操作示例如下:

Class rootClass = NSClassFromString(@"Person"); 

if (rootClass) {
id object = [[rootClass alloc] init];
[(Person *)object run];
}

4.3. 监控动态库的加载和移除

_dyld_register_func_for_add_image(&image_added);
_dyld_register_func_for_remove_image(&image_removed);

5. 其它

5.1. framework 里面的资源

资源放置在 Aggregate->Build Phases ->Target Dependencies

引用资源类似这样:VoogolfZxing.framework/CodeScan.bundle/qrcode_Scan_weixin_Line

iOS 开发新版 动态库framework的更多相关文章

  1. ios 开发中 动态库 与静态库的区别

    使用静态库的好处 1,模块化,分工合作 2,避免少量改动经常导致大量的重复编译连接 3,也可以重用,注意不是共享使用 动态库使用有如下好处: 1使用动态库,可以将最终可执行文件体积缩小 2使用动态库, ...

  2. iOS开发中静态库之".framework静态库"的制作及使用篇

    iOS开发中静态库之".framework静态库"的制作及使用篇 .framework静态库支持OC和swift .a静态库如何制作可参照上一篇: iOS开发中静态库之" ...

  3. WWDC2014之iOS使用动态库 framework【转】

    from:http://www.cocoachina.com/industry/20140613/8810.html JUN 12TH, 2014 苹果的开放态度 WWDC2014上发布的Xcode6 ...

  4. iOS开发之静态库(五)—— 图片、界面xib等资源文件封装到静态框架framework

    编译环境:Macbook Air + OS X 10.9.2 + XCode5.1 + iPhone5s(iOS7.0.3) 一.首先将资源文件打包成bundle 由于bundle是静态的,所以可以将 ...

  5. iOS开发中静态库制作 之.a静态库制作及使用篇

    iOS开发中静态库之".a静态库"的制作及使用篇 一.库的简介 1.什么是库? 库是程序代码的集合,是共享程序代码的一种方式 2.库的类型? 根据源代码的公开情况,库可以分为2种类 ...

  6. iOS开发中与库相关的术语

    动态库 VS 静态库 Static frameworks are linked at compile time. Dynamic frameworks are linked at runtime

  7. 分享:写了一个 java 调用 C语言 开发的动态库的范例

    分享:写了一个 java 调用 C语言 开发的动态库的范例 cfunction.h   代码#pragma once#ifdef __cplusplusextern "C" {#e ...

  8. 李洪强iOS开发之静态库的打包一

    李洪强iOS开发之静态库的打包一 //静态库一般做一下几种事情 //1 工具类 算法逻辑 新建工具类LHQTools 定义类方法 + (NSInteger)sumWithNum1: (NSIntege ...

  9. 李洪强iOS开发之动态获取UILabel的bounds

    李洪强iOS开发之动态获取UILabel的bounds 在使用UILabel存放字符串时,经常需要获取label的长宽数据,本文列出了部分常用的计算方法. 1.获取宽度,获取字符串不折行单行显示时所需 ...

随机推荐

  1. Oracle如何还原数据库

    http://blog.sina.com.cn/s/blog_6c25b1e50101ga0b.html

  2. java数据类型,hibernate数据类型,标准sql数据类型之间的对应表

    Hibernate API简介 其接口分为以下几类: l         提供访问数据库的操作的接口: l         用于配置Hibernate的接口: l         回调接口 l     ...

  3. strut1.X和spring整合的二种方法

    第一种集成方法 原理:在Action中取得BeanFactory对象,然后通过BeanFactory获取业务逻辑对象 缺点:产生了依赖,spring的类在action中产生了依赖查找.(注意和依赖注入 ...

  4. Barnicle

    Barnicle Barney is standing in a bar and starring at a pretty girl. He wants to shoot her with his h ...

  5. 2304: Lights Out(枚举)

    枚举第一行所有可能的的情况 #include<iostream> #include<cstdio> #include<cstring> #include<al ...

  6. ASCII,Unicode 和通用方式

    ASCII码 字符char,字符指针char*,字符数组char a[]; 例如:char a='A'; char* pC="beijing"; char aC[]="b ...

  7. Codeforces Round #369 (Div. 2) D. Directed Roads (DFS)

    D. Directed Roads time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  8. Android4.4KitKat支持u盘功能

    Android4.4KitKat支持u盘功能 作者:  发布日期:2014-05-14 23:16:13 我来说两句(0) 0 Tag标签:功能   Android U 盘功能实现和分析 u 盘功能实 ...

  9. jQuery学习笔记—— .html(),.text()和.val()的使用

    本节内容主要介绍的是如何使用jQuery中的.html(),.text()和.val()三种方法,用于读取,修改元素的html结构,元素的文本内容,以及表单元素的value值的方法.jQuery中为我 ...

  10. Memcached缓存系统介绍及安装

    1.什么是Memcached 1.1.Memcached概述 Memcached是一个免费的开源的.高性能的.具有又分布式内存对象的缓存系统,它通过减轻数据库负载加速动态WEB应用, 1.2.Memc ...