iOS静态库(.framework)

2017-11-16  本文已影响0人  iOSLee

本文是基于Xcode9的开发环境搭建的,由于本人使用的.framework形式,所以只说.framework形式的静态库,其他可自行百度

提交appstore的应用使用的库不允许包含i386或x86_64,否则会报错

前提

库:库实际上是一种代码共享的方式,主要用于代码重用和源码隐藏,通常分为动态库和静态库。

静态库:链接时完整的拷贝至可执行文件中,被多次使用就有多份冗余拷贝。

动态库:链接时不复制,程序运行时由系统动态加载到内存,供程序调用,系统只加载一次,多个程序共用,节省内存空间。

静态库和动态库的存在形式

静态库:.a 和 .framework

动态库:.dylib 和 .framework

支持多个设备需要了解Xcode指令集及配置,请跳转

制作

一、创建工程

1、打开Xcode在导航栏选择File -》New -》Project,选择Cocoa Touch Framework,点击Next创建工程

2、新建工程后目录结构如下(TestApi是我自己添加的文件),在Targets -》Build Phases -》 Headers 选择对外暴露的头文件,Private为空则只暴露Public

3、配置Targets -》Build Settings

(1)使静态库支持bitcode,需要在Other C Flags添加-fembed-bitcode

(2)更改Mach-0 Type 为Static Library (默认创建的是Dynamic)

二、添加Aggregate Target

1、File -》New -》Target,选择Cross-platform中的Aggregate并创建

2、选择新加的Aggregate Target -》Build Phases 点击“+”号选择New Run Script Phase添加脚本

脚本内容:

# 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}"

三、制作通用静态库

方案一、Build的Target使用二中创建的即可,如下图:

方案二、使用Carthage中的使你的framework支持Carthage章节

上一篇 下一篇

猜你喜欢

热点阅读