Xcode 7制作动态Framework

动态Framework

1、新建一个工程,左边选择iOS-Framework & Library 然后选择Cocoa Touch Framework。

2、现在可以创建所需要封装的类和方法。

3、选中工程target里面的Build Phases,然后选择Build Phases下的 Headers选项,若没有该选项,则可通过Xcode的Editor-Add Build Phase-Add Headers Build Phase添加(若该项不能选择,请先点击Build Phases下面的空白处,激活该选项)。

4、把需要对外开放的.h文件从Headers的Project里面拖到Public里面。

5、新工程里面会有一个与工程同名的.h文件,需要在这个文件里面导入对外开放的.h文件(#imporf<工程名/xxx.h>)。

6、选择target里面的Build Settings,在Architectures里面添加Framework支持的架构,默认支持armv7、arm64,现在需要增加armv7s。

7、选择模拟器里面的Generic iOS Device ,记得先clean一下在run。

8、点开Xcode左边导航栏的Products,现在可以看到里面的Framework由红色变成了黑色,选中它然后show in Finder, 把它copy到桌面。

9、打开终端,我们来测试一下新建的Framework支持哪些架构。

  
cd Desktop 
lipo -info FFrameworkT.framework/FFrameworkT
Architectures in the fat file: FFrameworkT.framework/FFrameworkT are:     armv7 armv7s arm64   

10、现在新建一个single view 工程,选中target里面的General-Embeded Binaries,点击里面的+号,选择 Add oother… ,找到桌面的新建的Framework,然后照常使用。

11、选择任意一个模拟器,run,这时出现了如下错误:

ld: warning: ignoring file /Users/AZmake/Desktop/testFFFF/FFrameworkT.framework/FFrameworkT, file was built for armv7 which is not the architecture being linked (x86_64): /Users/AZmake/Desktop/testFFFF/FFrameworkT.framework/FFrameworkT

Undefined symbols for architecture x86_64:

“_FFrameworkTVersionNumber”, referenced from:

  -[ViewController viewDidLoad] in ViewController.o  
  

“_FFrameworkTVersionString”, referenced from:

  -[ViewController viewDidLoad] in ViewController.o
    

“_OBJC_CLASS_$_love”, referenced from:

  objc-class-ref in ViewController.o  

ld: symbol(s) not found for architecture x86_64

clang: error: linker command failed with exit code 1 (use -v to see invocation)

这是什么原因??????
因为新建的Framework不支持模拟器的x86_64架构,现在这个Framework只能在真机上运行。
我想要在模拟器上运行,怎么办??????

让 Framework 同时支持模拟器和真机

1、选中Framework的target,通过Xcode的Editor-Add target… , 然后选择最下面的Other – Aggregate,如何Framework的名字。

2、选中新添加的target(有个靶一样的图标),Build Phases,点击+号添加 Run Script,双击改行把名字 Run Script 改为工程的名字。

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 "${INSTALL_DIR}"

4、现在把要编译的项目改为我们添加的target,选择模拟器里面的Generic iOS Device ,记得先 clean 一下在run。若没有错误,完成后会有Finder弹窗出现,现在一个同时支持模拟器和真机的
Framework就完成了。我们可以在项目的Products里面找到Framework。

5、把Framework复制到桌面看看它支持哪些架构。


cd Desktop/ 
lipo -info AZFrameworks.framework/AZFrameworks
Architectures in the fat file: AZFrameworks.framework/AZFrameworks are: i386 x86_64 armv7 armv7s arm64 

现在可以看到Framework支持i386 x86_64 armv7 armv7s arm64 这些架构。

    原文作者:AZmake
    原文地址: https://segmentfault.com/a/1190000004538382
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞