代码改变世界

iOS 库 开发小结

2017-04-12 20:34  v2m  阅读(972)  评论(0编辑  收藏  举报

1.基本用法

定义类,导出头文件,注意头文件,库文件的search path

2.加载资源

- 使用主工程的文件,耦合性太强
- 封装到NSBundle中
	NSBundle可以封装xib storyboard assets
	对于png文件bundle 对于 不同scale的png文件会默认合并成tiff文件,如果不想这样可以把bundle的build settings里面的 combine_hidpi_images 设置为NO

3.动态 framework

静态库直接链接到程序文件中,动态库就可以分开单独的文件,并且可选择的链接加载,如果不是刚启动就加载可以降低启动时间。

动态库加载方式

链接选项设置 require 或者 option

  1. 如果没有设置可以用 dlopen 的方式动态加载
NSString *documentsPath = [NSString stringWithFormat:@"%@/Documents/Dylib.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.");
    }
}
  1. 使用NSBundle的方式来加载
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.链接选项

-all_load 链接所有文件,不管是否会调用里面的符号
-force_load 对指定的库,链接所有文件
-ObjC 如果文件里面有OC代码,就链接这个文件
Perform Single-Object PreLink 这个选项是对库工程设置的,如果启用这个选项,所有的对象文件都会被合并成一个单文件

-whyload 编译日志中会记录那个文件因为什么原因而加载。但是它只会打印第一个被认为是“使用中”的符号。

-dead_strip 这个选项会移除那些虽然和文件一起加载了,但是没有没用的代码和数据。
Dead strip对于C代码能很好的工作(例如:像预期的那样去掉没用的函数、变量和常量),它在C++上也能工作的不错(例如:没用的类能够被移除)。虽然它并不完美,在一些情况下一些符号没有被移除,但是在大多数情况下它能在这些语言下很好地工作。

5.NSBundle 相关

  1. 根据类名得到 bundle
NSBundle *bundle = [NSBundle bundleForClass:[DemoViewController class]];
  1. 根据路径得到
NSString *path = [[NSBundle mainBundle] pathForResource:@"xx" ofType:@"framework" inDirectory:@"Frameworks"];
NSBundle *bundle = [NSBundle bundleWithPath:path];
  1. 多语言
NSBundle* englishBundle = [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:@"en" ofType:@"lproj"]];
NSString* englishTranslation = [englishBundle localizedStringForKey:translation_key value:@"" table:nil];

en_GB.lproj/
en_US.lproj/
en.lproj/
对于多语言资源的查找,遵循下面的逻辑

- 先查找 语言+地区
- 然后 语言
- 最后 公共

7.相关命令

lipo 用来查询库的编译对应平台,以及整合多个版本的库文件到一个库文件

nm 查询库文件的符号文件

ibtool xib文件编译,国际化文字提取相关命令,可以用来把 xib 文件编译成 nib 文件

$ ibtool --errors --warnings --output-format human-readable-text --compile file.nib file.xib

8.BitCode

BitCode是编译期的feature,而非链接期的feature,也就是编译过程中每个.o文件都会有一个叫做__bitcode的段落生成。

在Build Options中启用BitCode,且使用Build而非Archive编译时,Xcode会自动添加编译选项-fembed-bitcode-marker,这个选项的意思大概就是说:如果BitCode开启的话,这里本来应当是放bitcode的,实际上没放。

在Build Options中启用BitCode,且使用Archive编译时,Xcode会自动添加编译选项-fembed-bitcode,此时才是真正开启了BitCode。

如果编译选项设置-fembed-bitcode-marker,编译成功后上传iTunes Connect,就会出现错误。

如果使用Build编译想强制开启-fembed-bitcode,只需在Target->Build Settings->OTHER_CFLAGS中加入-fembed-bitcode即可。此时Build编译会同时出现两个参数 -fembed-bitcode-marker-fembed-bitcode

9.打包

一般网上都是 Aggregate target 方式,但实际上这个只是用来一次编译多个target用的,最终还是要用脚本,既然如此,那不如一步到位直接用脚本解决。我写了一个 脚本 来解决多平台库文件打包问题。
使用方式如下

./package.sh AFrameWorkTarget framework
./package.sh ALIBTarget

参考1.[http://www.galloway.me.uk/tutorials/ios-library-with-resources/]
参考2.[http://www.cocoachina.com/ios/20170401/18989.html]
参考3.[http://stackoverflow.com/questions/12244494/image-resources-for-ios]
参考4.[http://foggry.com/blog/2014/06/12/wwdc2014zhi-iosshi-yong-dong-tai-ku/]
参考5.[http://www.cocoachina.com/ios/20170401/18989.html]
参考6.[http://stackoverflow.com/questions/2567498/objective-c-categories-in-static-library]
参考7.[http://www.jianshu.com/p/fc6b6b43e979]
参考8.[http://stackoverflow.com/questions/31486232/how-do-i-xcodebuild-a-static-library-with-bitcode-enabled]