Xcode6制作动态及静态Framework[repost]

简介:

有没有写SDK或者要将一些常用的工具类做成Framework的经历? 你或许自己写脚本完成了这项工作,相信也有很多的人使用 iOS-Universal-Framework ,随着xCode6的发布,相信小伙伴们已经都知道了,xCode6支持做Framework了. 同时iOS-Universal-Framework开发者也宣布不在继续维持此项目的开发,建议开发者使用xCode6制作,目前网上也有很多制作iOS Framework的资料,但大多都不够详细,接下来本文会详情介绍一下在xcode6下制作iOS Framework.


关于静态库和动态库的概念,网上资料很多,这里不做叙述,只讲解制作过程。

创建iOS动态库

新建工程并选择默认Target为Cocoa Touch Framework, 如图:

做编码工作,在这里我简单的写了一个Utils的类,并写了一个log方法

设置开放的头文件:Framework中有些类可能是一些私有的辅助工具,不需要使用者看到,在这里只需要把开放出去的类放到Public下, 如图

这样生成的Framework的Headers目录下也只能看到Public的头文件

编码完成之后,直接Run就能成功生成Framework文件了,选择 xCode->Window->Organizer->Projects->Your Project, 打开工程的Derived Data目录,这样就能找到生成的Framework文件了,如图

新建测试工程,使用生成的Framework

将Framework文件导入到测试工程,调用Framework中的代码

 

MyUtils *utils = [MyUtils new]; 
[utils log:@"didFinishLaunchingWithOptions"];

运行报错(Reason: Image Not Found)

为什么会这样的?因为我们做的是动态库,在使用的时候需要额外加一个步骤,要把Framework同时添加到‘Embedded Binaries’中

注意: 在xCode6之前是没有这个选项的(我没发现),所以理论上xCode5及之前的版本无法使用xCode6下生成的Framework动态库

到这里,假定你整个过程都是使用的模拟器做的,那看上去会很顺利。这时候尝试将测试工程部署到真机上,问题来了

ld: warning: ignoring file /work/ios/MyFrameworkTest/MyFrameworkTest/MyFramework.framework/MyFramework, file was built for x86_64 which is not the architecture being linked (armv7): /work/ios/MyFrameworkTest/MyFrameworkTest/MyFramework.framework/MyFramework
Undefined symbols for architecture armv7:
  "_OBJC_CLASS_$_MyUtils", referenced from:
      objc-class-ref in AppDelegate.o
ld: symbol(s) not found for architecture armv7
clang: error: linker command failed with exit code 1 (use -v to see invocation)

为什么会这样?错误提示已经很明显了,因为我们制作动态库的时候,选的设备是模拟器,如果选真机的话,那生成的库也只能在真机上使用,那我们该怎样制作一个通用的动态库呢? 简单的方法是分别生成模拟器和真机上运行的库,然后在合并,这个方法,在每次生成动态库的时候,过程都会很繁琐,下面我们用一个脚本来自动完成它。

制作通用动态库

新建Aggregate Target

添加script到新建的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=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}"

选中新建的Target,Run, 如果没有异常的话,会自动弹出生成的Framework文件

这样生成的动态库就能同时支持模拟器和真机了

 

xCode6下制作通用静态库

上面我们也提到了,这样生成的动态库恐怕很难在xCode5上使用,那我们为什么非要用动态库呢,一般情况下不是用静态库就好了吗? So Easy!只需要修改一个参数即可生成静态库了

使用静态库的话,就可以把Framework从‘Embedded Binaries’中删除了. 亲测在xCode5下可用。把新生成的库导入到测试工程,试试在模拟器和真机上运行,一切OK.

不巧,如果你用的真机是iPhone5 C, 那悲剧又要发成了,生成的Framework竟然不支持armv7s,不知是xCode6的bug,还是因为苹果认为使用armv7s的设备太少,可以不支持了.xCode6新建工程,默认的Architectures竟然不包含armv7s

想要生成的库支持armv7s,把armv7s添加到Architectures中,重新生成Framework即可

判断一个Framework支持哪些架构

我们该怎么验证生成的Framework支持哪些平台呢,总不能一个个测试吧?当然不用.下面的命令是加上armv7s前后生成的framework的对比

Yearsdembp:Products Years$ lipo -info ./MyFramework.framework/MyFramework 
Architectures in the fat file: ./MyFramework.framework/MyFramework are: i386 x86_64 armv7 arm64 
Yearsdembp:Products Years$ lipo -info ./MyFramework.framework/MyFramework 
Architectures in the fat file: ./MyFramework.framework/MyFramework are: armv7 armv7s i386 x86_64 arm64 
欢迎加群互相学习,共同进步。QQ群:iOS: 58099570 | Android: 330987132 | Go:217696290 | Python:336880185 | 做人要厚道,转载请注明出处!http://www.cnblogs.com/sunshine-anycall/p/4043164.html
相关文章
|
2月前
|
Linux 数据安全/隐私保护 iOS开发
如何使用 Xcode 打包导出 IPA 文件并进行 iOS 应用内测,无需支付苹果开发者账号费用?
如何使用 Xcode 打包导出 IPA 文件并进行 iOS 应用内测,无需支付苹果开发者账号费用?
|
存储 XML jenkins
iOS 底层原理38:自动化打包(一)Xcode + Shell脚本
iOS 底层原理38:自动化打包(一)Xcode + Shell脚本
2426 1
iOS 底层原理38:自动化打包(一)Xcode + Shell脚本
|
8月前
|
iOS开发
iOS Xcode 意外退出 打不开工程
iOS Xcode 意外退出 打不开工程
106 0
|
6月前
|
iOS开发 芯片 MacOS
[Xcode 12, building for iOS Simulator, but linking in object file built for iOS, for architecture...
[Xcode 12, building for iOS Simulator, but linking in object file built for iOS, for architecture...
123 0
|
6月前
|
iOS开发 开发者
Xcode7.2真机调试iOS 9.3的设备
Xcode7.2真机调试iOS 9.3的设备
108 0
|
9月前
|
iOS开发
Xcode 12.3 编译提示Building for iOS Simulator, but the linked and embedded framework
Xcode 12.3 编译提示Building for iOS Simulator, but the linked and embedded framework
|
11月前
|
Linux 数据安全/隐私保护 iOS开发
如何使用 Xcode 打包导出 IPA 文件并进行 iOS 应用内测,无需支付苹果开发者账号费用?
苹果开发者账号认证需要支付 688 元,对于初学 iOS 开发的同学来说,仅仅是为了进行内测而不需要上架 App Store,这样的费用是不必要的。
|
11月前
|
Linux 数据安全/隐私保护 虚拟化
Ios上架app无需688,xcode打包导出IPA,供人内测
无需支付688苹果开发者账号,xcode打包导出ipa,提供他人进行内测 众所周知,在开发苹果应用时需要使用签名(证书)才能进行打包安装苹果IPA,作为刚接触ios开发的同学,只是学习ios app开发内测,并没有上架appstore需求
|
C语言 iOS开发
Agreeing to the Xcode/iOS license requires admin privileges, please run “sudo xcodebuild -license”
Agreeing to the Xcode/iOS license requires admin privileges, please run “sudo xcodebuild -license”
84 0
|
编译器 开发工具 C语言
iOS 语言基础&初探 Xcode 工具
iOS 语言基础&初探 Xcode 工具
186 0
iOS 语言基础&初探 Xcode 工具