第一步:制作framework
2)创建我们将要封装的类,eg:创建一个MyObject类,代码如下:
@interface MyObject : NSObject
-(
void
)myLog;
@end
@implementation MyObject
-(
void
)myLog{
NSLog(@
"framework"
);
}
@end
3)新建Target
然后选择Aggregate:
之后我们在target的Build Phases中点击加号:
增加一个Run Script:
改变运行的Target ,填写下边的shell脚本
在里面添加如下的脚本:
<code class="bash" style="padding:0px; font-family:Menlo,Monaco,Consolas,'Courier New',monospace; font-size:12px; border:none; background-color:transparent"><span class="hljs-comment" style="color:#93a1a1;"># Sets the target folders and the final framework product.</span> <span style="color:#93a1a1;"></span><span class="hljs-comment" style="color:#93a1a1;"># 如果工程名称和Framework的Target名称不一样的话,要自定义FMKNAME</span> <span class="hljs-comment" style="color:#93a1a1;"># 例如: FMK_NAME = "MyFramework"</span> FMK_NAME=<span class="hljs-variable" style="color:#b5890;">${PROJECT_NAME}</span> <span class="hljs-comment" style="color:#93a1a1;"># Install dir will be the final output to the framework.</span> <span class="hljs-comment" style="color:#93a1a1;"># The following line create it in the root folder of the current project.</span> INSTALL_DIR=<span class="hljs-variable" style="color:#b5890;">${SRCROOT}</span>/Products/<span class="hljs-variable" style="color:#b5890;">${FMK_NAME}</span>.framework <span class="hljs-comment" style="color:#93a1a1;"># Working dir will be deleted after the framework creation.</span> WRK_DIR=build DEVICE_DIR=<span class="hljs-variable" style="color:#b5890;">${WRK_DIR}</span>/Release-iphoneos/<span class="hljs-variable" style="color:#b5890;">${FMK_NAME}</span>.framework SIMULATOR_DIR=<span class="hljs-variable" style="color:#b5890;">${WRK_DIR}</span>/Release-iphonesimulator/<span class="hljs-variable" style="color:#b5890;">${FMK_NAME}</span>.framework <span class="hljs-comment" style="color:#93a1a1;"># -configuration ${CONFIGURATION}</span> <span class="hljs-comment" style="color:#93a1a1;"># Clean and Building both architectures.</span> xcodebuild -configuration <span class="hljs-string" style="color:#2aa198;">"Release"</span> -target <span class="hljs-string" style="color:#2aa198;">"<span class="hljs-variable" style="color:#b5890;">${FMK_NAME}</span>"</span> -sdk iphoneos -arch armv7 -arch armv7s -arch arm64 clean build xcodebuild -configuration <span class="hljs-string" style="color:#2aa198;">"Release"</span> -target <span class="hljs-string" style="color:#2aa198;">"<span class="hljs-variable" style="color:#b5890;">${FMK_NAME}</span>"</span> -sdk iphonesimulator -arch x86_64 clean build <span class="hljs-comment" style="color:#93a1a1;"># Cleaning the oldest.</span> <span class="hljs-keyword" style="color:#85990;">if</span> [ <span class="hljs-operator">-d</span> <span class="hljs-string" style="color:#2aa198;">"<span class="hljs-variable" style="color:#b5890;">${INSTALL_DIR}</span>"</span> ] <span class="hljs-keyword" style="color:#85990;">then</span> rm -rf <span class="hljs-string" style="color:#2aa198;">"<span class="hljs-variable" style="color:#b5890;">${INSTALL_DIR}</span>"</span> <span class="hljs-keyword" style="color:#85990;">fi</span> mkdir -p <span class="hljs-string" style="color:#2aa198;">"<span class="hljs-variable" style="color:#b5890;">${INSTALL_DIR}</span>"</span> cp -R <span class="hljs-string" style="color:#2aa198;">"<span class="hljs-variable" style="color:#b5890;">${DEVICE_DIR}</span>/"</span> <span class="hljs-string" style="color:#2aa198;">"<span class="hljs-variable" style="color:#b5890;">${INSTALL_DIR}</span>/"</span> <span class="hljs-comment" style="color:#93a1a1;"># Uses the Lipo Tool to merge both binary files (i386 + armv6/armv7) into one Universal final product.</span> lipo -create <span class="hljs-string" style="color:#2aa198;">"<span class="hljs-variable" style="color:#b5890;">${DEVICE_DIR}</span>/<span class="hljs-variable" style="color:#b5890;">${FMK_NAME}</span>"</span> <span class="hljs-string" style="color:#2aa198;">"<span class="hljs-variable" style="color:#b5890;">${SIMULATOR_DIR}</span>/<span class="hljs-variable" style="color:#b5890;">${FMK_NAME}</span>"</span> -output <span class="hljs-string" style="color:#2aa198;">"<span class="hljs-variable" style="color:#b5890;">${INSTALL_DIR}</span>/<span class="hljs-variable" style="color:#b5890;">${FMK_NAME}</span>"</span> rm -r <span class="hljs-string" style="color:#2aa198;">"<span class="hljs-variable" style="color:#b5890;">${WRK_DIR}</span>"</span> open <span class="hljs-string" style="color:#2aa198;">"<span class="hljs-variable" style="color:#b5890;">${SRCROOT}</span>/Products/"</span></code>
之后我们运行程序,需要注意的一点事,如果要支持64位,需要在编译选项中设置,如下:
到此时,我们的framework库文件就制作完成,在xcode的window->projects中选中我们的这个项目,点击进入文件夹的小箭头:
product中便可以找到我们的framework文件,我们将其赋值出来即可以使用。到此我们的framework就制作完成了
我们测试一下,新建一个工程,将刚才制作的静态库导入,如下加入头文件,调用方法,可以使用:
#import <MyFramework/MyObject.h>
MyObject * obj = [[MyObject alloc]init];
[obj myLog];
第二步:解决会遇到的问题:
原文链接:XCode6添加自定义framework运行真机出现dyld: Library not loaded的解决方法
1、直接添加上面制作好的framework,会发报如下错误:
dyld: Library not loaded: @rpath/RLLibrary.framework/RLLibrary
Referenced from: /var/mobile/Applications/AE92B234-A818-445E-9D69-96E232BD50EB/RLProjectDemo.app/RLProjectDemo
Reason: image not found
(lldb)
讨论:看网上不少人说是将上图Link Binary With Libraries中的Required改为Optional就没事了,实际真机运行发现不是回事,即使不报错了,但是也是无法运行程序。
解决方法:仔细看错误原因,应该是没有找到framework文件包,So,怎么编译时打包进去呢?如下图,点击那个+号:
添加Copy Files节点,在Destination中选Frameworks,再点击下面+号,选择自定义的framework,ok,真机 Build,Run,这个世界美好了。
2、即使上面都已经配置好了,程序运行ok,但是当引用库中.h文件时,会警告“missing submodule 'XXXframework'”
解决方法:创建库文件时,只要在库文件中与库同名.h文件中添加要暴露的.h文件,有点绕,可以看下图: