iOS 引入framework的常见问题和原理

今天在引入第三方framework时,我按照以前的方法,把framework加入到了下图的地方:

默认是required的,之后程序就crash了,报错dyld: Library not loaded,之后我把required 改成了optional,这次不crash了,但是运行时出错,库中的对象无法实例化,全部是空。

我是根据http://stackoverflow.com/questions/24333981/ios-app-with-framework-crashed-on-device-dyld-library-not-loaded-xcode-6-beta解决的,就是利用Embeded Binaries选项。 

 

先看看optional 和 required 有什么区别,如下是官方的说法:

Libraries and frameworks are designated as Required by default, but you can change this designation to Optional. Required libraries must be present on the host computer for the product to load. Optional libraries do not have to be present for the product to load. A Required framework will always be loaded into memory, but an Optional framework will be loaded only if needed. The initial load of the application will be faster if a large library that is never needed is designated as Optional.

 在看看framework的官方说明:

A framework is a hierarchical directory that encapsulates shared resources, such as a dynamic shared library, nib files, image files, localized strings, header files, and reference documentation in a single package. Multiple applications can use all of these resources simultaneously. The system loads them into memory as needed and shares the one copy of the resource among all applications whenever possible.

注意,根据上面的介绍,看起来framework中只能包含 dynamic shared library 2进制文件,其实不是,framework 中也可以包含静态库2进制文件!通过下面这个参数来指定:

这里如果指定为 Static Library(framework中包含的2进制文件就是个静态库),那么当这个framework就可以只加入Linked Frameworks and Libraries 选项卡中,系统会自动识别它的类型,并链接入目标文件。而如果指定为Dynamic Library(framework 中包含的2进制文件就是个动态库) ,那么,加入到 Linked Frameworks and Libraries 选项卡中是不够的,而是需要加入到Embeded Binaries选项卡中,加入后系统会自动为你在Linked Frameworks and Libraries选项卡里加入相同framework,打开对应的ipa,会看到 framework在单独的一个文件夹中。我这里的理解是,Linked Frameworks and Libraries表示连接,动态连接也算连接,所以Embeded Binaries选项卡中中的项目也会出现在Linked Frameworks and Libraries选项卡中。

另外,如果framework中包含的2进制文件是个静态库,而我们把这个framework当成一个包含动态库的framework,添加到了Embeded Binaries选项卡中,那么程序无法通过编译,会报错的。

如何看一个framework中的2进制文件是一个静态库还是一个动态库呢?使用file 命令。

见下面的截图,一个动态库,一个静态库:

 

看到这里可以明白了,framework就是把以前的.a 和 .so 文件又包装了一层目录,加入了些其他必要的文件,比如以前用.a时需要单独引入的一系列.h文件,这样看起来更规范一些!

 

这里还有一篇好的博文 http://www.cnblogs.com/huizhang212/p/lipolib.html,说的是如何从.a除去不要的.o文件。

 

 

就是说,如果引入了第三方的动态库,这个包含动态库的framework就需要添加到 ipa包中,加载的时机看参数,如果是required,那么程序载入时就加载,如果是optional,那么是使用到时,才加载。如果引入了静态库,这个.a的链接也看参数?不都是需要把部分2进制代码链接入目标文件吗?有什么区别吗?不太明白。我觉得当使用静态库时,这个required 和 optional就没什么用了。

关于创建自定义framework,需要仔细研究一下这篇文章 http://www.cocoachina.com/ios/20141126/10322.html

 


 

又遇到了一个问题,用embeded 方式加入 lumberjack framework 后,运行时总出如下警告:

objc[2738]: Class DDTTYLogger is implemented in both /private/var/containers/Bundle/Application/621285FC-47C0-4631-A664-2764018435AD/Gamification.app/Frameworks/CocoaLumberjack.framework/CocoaLumberjack and /var/containers/Bundle/Application/621285FC-47C0-4631-A664-2764018435AD/Gamification.app/Gamification. One of the two will be used. Which one is undefined.

我搜索了工程源码,没有发现其他定义这个类的地方,之后,为了测试,我删掉了 lumberjack framework ,直接加入了lumberjack.a 进行编译,这次直接报错了!这次错误就写的很明白了,原来是工程引入的另一个.a中已经包括了lumberjack的源码,我又没有这个.a的源码,所以在工程中就搜索不到另一个定义。

这也是解决这种类似问题的小技巧吧。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值