iOS开发那些事无聊就做数学题-待学习环境集成

【Objective-c】开发封装动态库framework 装

2016-10-19  本文已影响1084人  MR_詹

开发中我们也经常会使用第三方SDK,只要几行代码就实现功能,重点是你看不到代码是怎么实现的,顿时感觉这个逼格很高,大牛一位。(面试中甚至有面试官问:你会不会封装SDK?当时心中忐忑不安,心想什么鬼,面试的是多少K的工作,居然要会SDK)

普及一下静态库和动态库的定义及区别

静态库即静态链接库(Windows 下的 .lib,Linux 和 Mac 下的 .a)。之所以叫做静态,是因为静态库在编译的时候会被直接拷贝一份,复制到目标程序里,这段代码在目标程序里就不会再改变了。静态库的好处很明显,编译完成之后,库文件实际上就没有作用了。目标程序没有外部依赖,直接就可以运行。当然其缺点也很明显,就是会使用目标程序的体积增大。

动态库动态库即动态链接库(Windows 下的 .dll,Linux 下的 .so,Mac 下的 .dylib)。与静态库相反,动态库在编译时并不会被拷贝到目标程序中,目标程序中只会存储指向动态库的引用。等到程序运行时,动态库才会被真正加载进来。动态库的优点是,不需要拷贝到目标程序中,不会影响目标程序的体积,而且同一份库可以被多个程序使用(因为这个原因,动态库也被称作共享库)。同时,编译时才载入的特性,也可以让我们随时对库进行替换,而不需要重新编译代码。动态库带来的问题主要是,动态载入会带来一部分性能损失,使用动态库也会使得程序依赖于外部环境。如果环境缺少动态库或者库的版本不正确,就会导致程序无法运行(Linux 下喜闻乐见的 lib not found 错误)。

创建动态库framework的步骤》》》》》》见图

1、新建项目


Snip20161019_1.png

2、将代码导入到项目中,并将公开的接口暴露


Snip20161019_2.png
注意:开放接口的头文件,一定要导入这个新建文件的头文件中,否则其他项目导入framework时会出现:找不到子模块的警告⚠️ Snip20161019_12.png

3、设置适合所有框架


Snip20161019_3.png

4、因为动态库打包出来只能是在真机或模拟器使用,为了方便调试,可以添加一个脚本命令,生成一个同时支持真机和模拟机的framework


![Snip20161019_5.png](http://upload-images.jianshu.io/upload_images/1923392-73cdcf4be0b4b5e1.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
Snip20161019_6.png
Snip20161019_7.png
脚本命令
set
 -e

set
 +u

### Avoid recursively calling this script.

if
 [[ $UF_MASTER_SCRIPT_RUNNING ]]

then

exit 0

fi

set
 -u

export UF_MASTER_SCRIPT_RUNNING=1

### Constants.

UF_TARGET_NAME=${PROJECT_NAME}

FRAMEWORK_VERSION=
"A"

UNIVERSAL_OUTPUTFOLDER=${BUILD_DIR}/${CONFIGURATION}-universal

IPHONE_DEVICE_BUILD_DIR=${BUILD_DIR}/${CONFIGURATION}-iphoneos

IPHONE_SIMULATOR_BUILD_DIR=${BUILD_DIR}/${CONFIGURATION}-iphonesimulator

### Functions

## List files in the specified directory, storing to the specified array.

#

# @param $1 The path to list

# @param $2 The name of the array to fill

#

##

list_files ()

{

filelist=$(ls 
"$1"
)

while
 read line

do

eval 
"$2[\${#$2[*]}]=\"\$line\""

done <<< 
"$filelist"

}

### Take build target.

if
 [[ 
"$SDK_NAME"
 =~ ([A-Za-z]+) ]]

then

SF_SDK_PLATFORM=${BASH_REMATCH[1]} # 
"iphoneos"
 or 
"iphonesimulator"
.

else

echo 
"Could not find platform name from SDK_NAME: $SDK_NAME"

exit 1

fi

### Build simulator platform. (i386, x86_64)

echo 
"========== Build Simulator Platform =========="

echo 
"===== Build Simulator Platform: i386 ====="

xcodebuild -project 
"${PROJECT_FILE_PATH}"
 -target 
"${TARGET_NAME}"
 -configuration 
"${CONFIGURATION}"
 -sdk iphonesimulator BUILD_DIR=
"${BUILD_DIR}"
 OBJROOT=
"${OBJROOT}"
 BUILD_ROOT=
"${BUILD_ROOT}"
 CONFIGURATION_BUILD_DIR=
"${IPHONE_SIMULATOR_BUILD_DIR}/i386"
 SYMROOT=
"${SYMROOT}"
 ARCHS=
'i386'
 VALID_ARCHS=
'i386'
 $ACTION

echo 
"===== Build Simulator Platform: x86_64 ====="

xcodebuild -project 
"${PROJECT_FILE_PATH}"
 -target 
"${TARGET_NAME}"
 -configuration 
"${CONFIGURATION}"
 -sdk iphonesimulator BUILD_DIR=
"${BUILD_DIR}"
 OBJROOT=
"${OBJROOT}"
 BUILD_ROOT=
"${BUILD_ROOT}"
 CONFIGURATION_BUILD_DIR=
"${IPHONE_SIMULATOR_BUILD_DIR}/x86_64"
 SYMROOT=
"${SYMROOT}"
 ARCHS=
'x86_64'
 VALID_ARCHS=
'x86_64'
 $ACTION

### Build device platform. (armv7, arm64)

echo 
"========== Build Device Platform =========="

echo 
"===== Build Device Platform: armv7 ====="

xcodebuild -project 
"${PROJECT_FILE_PATH}"
 -target 
"${TARGET_NAME}"
 -configuration 
"${CONFIGURATION}"
 -sdk iphoneos BUILD_DIR=
"${BUILD_DIR}"
 OBJROOT=
"${OBJROOT}"
 BUILD_ROOT=
"${BUILD_ROOT}"
  CONFIGURATION_BUILD_DIR=
"${IPHONE_DEVICE_BUILD_DIR}/armv7"
 SYMROOT=
"${SYMROOT}"
 ARCHS=
'armv7 armv7s'
 VALID_ARCHS=
'armv7 armv7s'
 $ACTION

echo 
"===== Build Device Platform: arm64 ====="

xcodebuild -project 
"${PROJECT_FILE_PATH}"
 -target 
"${TARGET_NAME}"
 -configuration 
"${CONFIGURATION}"
 -sdk iphoneos BUILD_DIR=
"${BUILD_DIR}"
 OBJROOT=
"${OBJROOT}"
 BUILD_ROOT=
"${BUILD_ROOT}"
 CONFIGURATION_BUILD_DIR=
"${IPHONE_DEVICE_BUILD_DIR}/arm64"
 SYMROOT=
"${SYMROOT}"
 ARCHS=
'arm64'
 VALID_ARCHS=
'arm64'
 $ACTION

### Build device platform. (arm64, armv7)

echo 
"========== Build Universal Platform =========="

## Copy the framework structure to the universal folder (clean it first).

rm -rf 
"${UNIVERSAL_OUTPUTFOLDER}"

mkdir -p 
"${UNIVERSAL_OUTPUTFOLDER}"

## Copy the last product files of xcodebuild command.

cp -R 
"${IPHONE_DEVICE_BUILD_DIR}/arm64/${PROJECT_NAME}.framework"
 "${UNIVERSAL_OUTPUTFOLDER}/${PROJECT_NAME}.framework"

### Smash them together to combine all architectures.

lipo -create  
"${BUILD_DIR}/${CONFIGURATION}-iphonesimulator/i386/${PROJECT_NAME}.framework/${PROJECT_NAME}"
 "${BUILD_DIR}/${CONFIGURATION}-iphonesimulator/x86_64/${PROJECT_NAME}.framework/${PROJECT_NAME}"
 "${BUILD_DIR}/${CONFIGURATION}-iphoneos/armv7/${PROJECT_NAME}.framework/${PROJECT_NAME}"
 "${BUILD_DIR}/${CONFIGURATION}-iphoneos/arm64/${PROJECT_NAME}.framework/${PROJECT_NAME}"
 -output 
"${UNIVERSAL_OUTPUTFOLDER}/${PROJECT_NAME}.framework/${PROJECT_NAME}"

### Create standard structure for framework.

#

# If we don't have "Info.plist -> Versions/Current/Resources/Info.plist", we may get error when use this framework.

#

# MyFramework.framework

# |-- MyFramework -> Versions/Current/MyFramework

# |-- Headers -> Versions/Current/Headers

# |-- Resources -> Versions/Current/Resources

# |-- Info.plist -> Versions/Current/Resources/Info.plist

# `-- Versions

#     |-- A

#     |   |-- MyFramework

#     |   |-- Headers

#     |   |   `-- MyFramework.h

#     |   `-- Resources

#     |       |-- Info.plist

#     |       |-- MyViewController.nib

#     |       `-- en.lproj

#     |           `-- InfoPlist.strings

#     `-- Current -> A

#

echo 
"========== Create Standard Structure =========="

mkdir -p 
"${UNIVERSAL_OUTPUTFOLDER}/${PROJECT_NAME}.framework/Versions/${FRAMEWORK_VERSION}/"

mv 
"${UNIVERSAL_OUTPUTFOLDER}/${PROJECT_NAME}.framework/${PROJECT_NAME}"
 "${UNIVERSAL_OUTPUTFOLDER}/${PROJECT_NAME}.framework/Versions/${FRAMEWORK_VERSION}/"

mv 
"${UNIVERSAL_OUTPUTFOLDER}/${PROJECT_NAME}.framework/Headers"
 "${UNIVERSAL_OUTPUTFOLDER}/${PROJECT_NAME}.framework/Versions/${FRAMEWORK_VERSION}/"

mkdir -p 
"${UNIVERSAL_OUTPUTFOLDER}/${PROJECT_NAME}.framework/Resources"

declare -a UF_FILE_LIST

list_files 
"${UNIVERSAL_OUTPUTFOLDER}/${PROJECT_NAME}.framework/"
 UF_FILE_LIST

for
 file_name 
in
 "${UF_FILE_LIST[@]}"

do

if
 [[ 
"${file_name}"
 == 
"Info.plist"
 ]] || [[ 
"${file_name}"
 =~ .*\.lproj$ ]]

then

mv 
"${UNIVERSAL_OUTPUTFOLDER}/${PROJECT_NAME}.framework/${file_name}"
 "${UNIVERSAL_OUTPUTFOLDER}/${PROJECT_NAME}.framework/Resources/"

fi

done

mv 
"${UNIVERSAL_OUTPUTFOLDER}/${PROJECT_NAME}.framework/Resources"
 "${UNIVERSAL_OUTPUTFOLDER}/${PROJECT_NAME}.framework/Versions/${FRAMEWORK_VERSION}/"

ln -sfh 
"Versions/Current/Resources/Info.plist"
 "${UNIVERSAL_OUTPUTFOLDER}/${PROJECT_NAME}.framework/Info.plist"

ln -sfh 
"${FRAMEWORK_VERSION}"
 "${UNIVERSAL_OUTPUTFOLDER}/${PROJECT_NAME}.framework/Versions/Current"

ln -sfh 
"Versions/Current/${PROJECT_NAME}"
 "${UNIVERSAL_OUTPUTFOLDER}/${PROJECT_NAME}.framework/${PROJECT_NAME}"

ln -sfh 
"Versions/Current/Headers"
 "${UNIVERSAL_OUTPUTFOLDER}/${PROJECT_NAME}.framework/Headers"

ln -sfh 
"Versions/Current/Resources"
 "${UNIVERSAL_OUTPUTFOLDER}/${PROJECT_NAME}.framework/Resources"

### Open the universal folder.

open 
"${UNIVERSAL_OUTPUTFOLDER}"

5、生成framework文件,选中“StyleBox”文件,运行程序


Snip20161019_9.png
Snip20161019_11.png

到这里,framework的创建已经成功了,只要右击“show in Finder”,就可以找得framework并导入其他项目中

Paste_Image.png

项目实际使用中,直接导入framework,并且需要动态库需要在此添加:


Snip20161019_14.png

当动态库中,包含资源文件如nib文件或图片的....


Snip20161019_13.png

以上是创建framework的基本流程

上一篇 下一篇

猜你喜欢

热点阅读