xcode6制作动态及静态framework

http://blog.csdn.net/andypan1314/article/details/41379083

http://stackoverflow.com/questions/24039470/xcode-6-ios-creating-a-cocoa-touch-framework-architectures-issues

http://blog.csdn.net/smking/article/details/24434819



有没有写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 
原文地址:http://years.im/Home/Article/detail/id/52.html











-----

举报

打开终端。输入pwd 先看一下当前所在的目当路径。使用ls 查看当前目录下的文件夹。

cd /users/[用户名]/Desktop/fmwksqlite/DerivedData/fmwksqlite/build/products/release-iphoneos/tgfmwk.framework (Heqin注:这里可能会需要的路径是framework/tgfmwk)

然后使用lipo -info tgfmwk 查看一下库的信息。

看到输出Non-fat file: tgfmwk is architecture: armv7

这里表明只支持真机。

同样的再来看看模拟器的。

cd /users/[用户名]/Desktop/fmwksqlite/DerivedData/fmwksqlite/build/products/release-iphonesimulator/tgfmwk.framework

然后使用lipo -info tgfmwk 查看一下库的信息。

看到输出Non-fat file: tgfmwk is architecture: i386

可见要想真机和模拟器都可以使用该framework需要将这两个版本的tgfmwk文件进行合并。

使用命令lipo -create xxxx/tgfmwk  xxxxx/tgfmwk  -output  tgfmwknew (Heqin:合并时,也是合并

lipo -create Release-iphonesimulator/BDLBSLogger.framework/Versions/A/BDLBSLogger Release-iphoneos/BDLBSLogger.framework/Versions/A/BDLBSLogger -output BDLBSLogger

合并完成后,再把BDLBSLogger替换上面simulator文件夹或者iphoneos对应目录下的一个,然后这个文件夹就是可以用来同时供i386和arm使用 )

其中xxxx表示路径,一个是真机的tgfmwk所在路径,一个是模拟器tgfmwk文件所在路径,然后输出tgfmwknew新的文件。输出后,只需要将这个tgfmwknew重命名为tgfmwk然后复盖原来的tgfmwk 文件即可。把复盖后的这个tgfmwk.framework考出来到具体使用的项目就可以实现真机和模拟器通用的framework 了。

看一下tgfmwknew的信息。

显示:Architectures in the fat file: tgfmwknew are : armv7  i386

可见这个合并的文件已具备了armv7(真机)和i386(虚拟机)的能力。

 

Heqin:另外, Google上有一个可以生成伪framework的工具,可以下载下来使用,比较方便








--------

up vote 33 down vote

Try the following steps to create a workspace that contains a framework project and an app project.

Workspace:

  • Create a Workspace.

Framework project:

  • Create an iOS Cocoa touch Framework project inside Workspace.
  • Add a simple Objective C class MyClass (header .h and implementation file .m), and create a method - (void)greetings.
  • Go project Build Phases > Headers > Move MyClass.h from Project to Public.
  • Change scheme to framework project and choose iOS simulator, then build. (Choose iOS Device if the app that integrates this framework runs on device. I will continue to use simulator in this example)
  • You should have no issue building, the framework build is found in your Derived Data directory which you can find in Organizer.

App project:

  • Create a Swift Single View application inside Workspace.
  • Drag above iOS simulator framework build (Found in Debug-iphonesimulator or Release-iphonesimulator) to your project.
  • Create a bridging header to expose Objective C class methods to Swift.
  • Import MyClass.h in bridging header file.
  • Note that if MyClass definition is not found, then add framework Headers path of to Build Settings Header Search Paths.
  • Create instance of MyClass in viewDidLoad of ViewController.swift, then call greetings.
  • Add framework to Target > Embedded Binaries
  • Change scheme to app project and choose iOS simulator, then build.
  • You should be able to see greeting messages.

Note that above example demonstrates how to build an app that runs in simulator. If you need to create universal static library that runs on both simulator and devices, then general steps are:

  • Build library for simulator
  • Build library for device
  • Combine them using lipo

There are good references on the web about it, here for example.

Create universal binary for framework: navigate to framework Derived Data directory then /Build/Products, following command should help you create a universal binary in Products directory:

lipo -create -output "framework-test-01-universal" "Debug-iphonesimulator/framework-test-01.framework/framework-test-01" "Debug-iphoneos/framework-test-01.framework/framework-test-01" 

Note that framework-test-01 is my framework project name.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值