iOS库 .a与.framework区别和制作

来源:互联网 发布:在win7下安装ubuntu 编辑:程序博客网 时间:2024/05/21 23:52

一、什么是库?

库是共享程序代码的方式,一般分为静态库和动态库。

二、静态库与动态库的区别?

静态库:链接时完整地拷贝至可执行文件中,被多次使用就有多份冗余拷贝。

动态库:链接时不复制,程序运行时由系统动态加载到内存,供程序调用,系统只加载一次,多个程序共用,节省内存。

三、iOS里静态库形式?

.a和.framework

四、iOS里动态库形式?

.dylib和.framework

五、framework为什么既是静态库又是动态库?

系统的.framework是动态库,我们自己建立的.framework是静态库。

六、a与.framework有什么区别?

.a是一个纯二进制文件,.framework中除了有二进制文件之外还有资源文件。

.a文件不能直接使用,至少要有.h文件配合,.framework文件可以直接使用。

.a + .h + sourceFile = .framework。

建议用.framework.

七、为什么要使用静态库?

方便共享代码,便于合理使用。

实现iOS程序的模块化。可以把固定的业务模块化成静态库。

和别人分享你的代码库,但不想让别人看到你代码的实现。

开发第三方sdk的需要。

八、制作静态库时的几点注意:

1 注意理解:无论是.a静态库还.framework静态库,我们需要的都是二进制文件+.h+其它资源文件的形式,不同的是,.a本身就是二进制文件,需要我们自己配上.h和其它文件才能使用,而.framework本身已经包含了.h和其它文件,可以直接使用。

2 图片资源的处理:两种静态库,一般都是把图片文件单独的放在一个.bundle文件中,一般.bundle的名字和.a或.framework的名字相同。.bundle文件很好弄,新建一个文件夹,把它改名为.bundle就可以了,右键,显示包内容可以向其中添加图片资源。

3 category是我们实际开发项目中经常用到的,把category打成静态库是没有问题的,但是在用这个静态库的工程中,调用category中的方法时会有找不到该方法的运行时错误(selector not recognized),解决办法是:在使用静态库的工程中配置other linker flags的值为-ObjC。

4 如果一个静态库很复杂,需要暴露的.h比较多的话,就可以在静态库的内部创建一个.h文件(一般这个.h文件的名字和静态库的名字相同),然后把所有需要暴露出来的.h文件都集中放在这个.h文件中,而那些原本需要暴露的.h都不需要再暴露了,只需要把.h暴露出来就可以了。(.h中包含的其它. h 文件都必须放在public中,否则打包称的静态库会会报错)。




创建一个依赖开发(Dependent Development)工程

可以在你的工程中导航到库工程了,这样做非常好,因为这样意味着你可以在库中编辑代码,并且运行示例工程来测试你做的改变。

不过在该工程中运行后生成的库,是在该工程的目录下生成,还是在静态库工程的目录下。(注意,使用下面脚本方法,则声称后直接弹出文件目录,不需考虑文件位置)


将framework工程直接拖拽进测试工程中,然后添加依赖关系。


多架构(Multi-Architecture)编译

iOS app需要在许多不同的CPU架构下运行:

  • arm7: 在最老的支持iOS7的设备上使用

  • arm7s: 在iPhone5和5C上使用

  • arm64: 运行于iPhone5S的64位 ARM 处理器 上

  • i386: 32位模拟器上使用

  • x86_64: 64为模拟器上使用



打包自己的framework(Xcode7&OSX10.11)兼容各种cpu类型



第一步:新建Framework项目

第二步:编写代码,构建完成项目

第三步:新建一个我们用来运行编译脚本的Target

第四步:在新建的Target里边添加一个脚本

第五步:填入脚本

第六步:运行并根据需要编译
# Sets the target folders and the final framework product.# 如果工程名称和Framework的Target名称不一样的话,要自定义FMKNAME# 例如: FMK_NAME = "MyFramework"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=buildDEVICE_DIR=${WRK_DIR}/Release-iphoneos/${FMK_NAME}.frameworkSIMULATOR_DIR=${WRK_DIR}/Release-iphonesimulator/${FMK_NAME}.framework# -configuration ${CONFIGURATION}# Clean and Building both architectures.xcodebuild -configuration "Release" -target "${FMK_NAME}" -sdk iphoneos -arch armv7 -arch armv7s -arch arm64 clean buildxcodebuild -configuration "Release" -target "${FMK_NAME}" -sdk iphonesimulator -arch x86_64 clean build# Cleaning the oldest.if [ -d "${INSTALL_DIR}" ]thenrm -rf "${INSTALL_DIR}"fimkdir -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/"

检测类库支持的版本(注意指向framework内部)

lipo -info ~/Documents/SFMapKit/Products/SFMapKit.framework/SFMapKit

在上面的脚本最后加入 (拷贝bundle和打包的framework在同一文件夹打开)

# Copy the resources bundle to **

ditto "${BUILT_PRODUCTS_DIR}/${FMK_NAME}.bundle" \

"${SRCROOT}/Products/${FMK_NAME}.bundle"



view controller 示例编码

在我的示例中,我建立了一个MyViewController视图控制器和获得一个放在bundle中的xib文件,这里从bundle中去除xib的代码和平时引用xib有点不同,代码如下:

@implementation MyViewController

-(id)init
{
    NSBundle *bundle=[NSBundle bundleWithURL:[[NSBundle mainBundle]URLForResource:@"MyViewController"     withExtension:@"xib"]];                      
    if (self=[super initWithNibName:@"MyViewController" bundle:bundle]) {        
    }
    return self;
}

唯一的不同是,我们获得从MyLibraryResources的bundle中获得xib是在库项目中创建的.

运行项目查看结果

 扩展<译>

 我发现对NSBundle写给category更方便访问bundle,可以不用写那么多代码,如下代码就可以了

@interface NSBundle (MyLibrary)+ (NSBundle*)myLibraryResourcesBundle;@end@implementation NSBundle (MyLibrary)+ (NSBundle*)myLibraryResourcesBundle {    static dispatch_once_t onceToken;    static NSBundle *myLibraryResourcesBundle = nil;    dispatch_once(&onceToken, ^{        myLibraryResourcesBundle = [NSBundle bundleWithURL:[[NSBundle mainBundle] URLForResource:@"MyLibraryResources" withExtension:@"bundle"]];    });    return myLibraryResourcesBundle;}@end

 然后控制器中的初始化方法改为

if ((self = [super initWithNibName:@"MyViewController" bundle:[NSBundle myLibraryResourcesBundle]])) {}

 另一个非常有用的category是UIImage,可以方便的获得bundle中的图片和重写图像

@implementation UIImage (MyLibrary)+ (UIImage*)myLibraryImageNamed:(NSString*)name;@end@implementation UIImage (MyLibrary)+ (UIImage*)myLibraryImageNamed:(NSString*)name {    UIImage *imageFromMainBundle = [UIImage imageNamed:name];    if (imageFromMainBundle) {        return imageFromMainBundle;    }    UIImage *imageFromMyLibraryBundle = [UIImage imageWithContentsOfFile:[[[NSBundle myLibraryResourcesBundle] resourcePath] stringByAppendingPathComponent:name]];    return imageFromMyLibraryBundle;}@end
参考:http://www.cocoachina.com/ios/20150127/11022.html

http://www.cnblogs.com/huntaiji/p/3488007.html

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


0 0
原创粉丝点击