iOS开发 制作Framework、封装SDK

一、Framework的制作

  1. 使用XCode创建Framework工程


    9062174-6f2f9d55451d735a.png
    使用XCode创建Framework工程
  2. 修改工程配置信息

a.Build Settings中Build Active Architecture Only设置为NO 
b.Build Settings中Mach将其设置成Static Library 静态库 
c.Build Settings中Architectures增加armv7s,如果有则略过
d.Build Settings中BITCODE设置为NO
e.如果SDK中使用了Category,一定要Build Settings中Other Linker Flags添加设置-ObjC -all_load
f.调整SDK所支持的最低版本(例如iOS 8.0)
  1. 将封装好的文件导入工程


    9062174-9bc2392fc3af1379.png
    将封装好的文件导入工程
  2. 新建PCH文件,并且配置好PCH文件(PCH文件可以放一些宏定义,然后将PCH文件引入XHToolKit.h文件中就可以用了)


    9062174-d3025bf737d429e7.png
    新建PCH文件
  3. 将封装类和PCH类的.h文件暴露到public中


    9062174-d59e94b76c67dcb3.png
    将封装类和PCH类的.h文件暴露到public中
  4. 你是否依赖了其他第三方?如AFN?我依赖了,所以为该工程加入Cocoapods


    9062174-40491108e4b76b86.png
    加入依赖的第三方库
  5. 制作Framework(此处讲解利用Shell脚本生成的Framework,有兴趣的可以了解一下Debug、Release真机模拟器合并)以下两步:

a.创建脚本Target
b.创建Shell脚本
c.拷贝Shell脚本代码至第三个图位置
d.一定要记住:修改工程运行模式为Release,这样编译出来的Framework就可以拿给别人真机测试了
9062174-112fba229dbe4357.png
创建脚本Target

9062174-720d73926b239691.png
加入Shell脚本

9062174-4d10fcdad2241254.png
图3: 加入Shell脚本代码

Shell脚本直接拷贝即可(以下代码)

# 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
BUNDLE_PATH=${SRCROOT}/IVUIKit/${FMK_NAME}.bundle
# Working dir will be deleted after the framework creation.

WRK_DIR=build

# clear work dir
rm -r "${WRK_DIR}"

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 build -UseModernBuildSystem=NO

xcodebuild -configuration "Release" -target "${FMK_NAME}" -sdk iphonesimulator build -UseModernBuildSystem=NO

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

cp -R "${BUNDLE_PATH}/" "${SRCROOT}/Products/${FMK_NAME}.bundle/"

open "${INSTALL_DIR}"
  1. 最后一步,删除工程中红色.a文件,不然编译会有错误,开始编译得到Framework

二、创建Cocoapods的测试工程---用来测试刚才制作的Framework

  1. 创建一个新工程,加入Cocoapods并且加入刚才依赖的第三方库(这处不详写了)
  2. 导入我们刚才生成的Framework文件
  3. 添加测试代码(测试Framework中的功能是否可以正常运行)
    需要注意的是,你可能会报错,怎么办呢?找到buildSetting---Allow Non-modular Includes In Framework Modules 这个值 改为YES.
    Build Settings中BITCODE设置为NO
    记住先导入cocoapods,编译一次,再去导入框架
  4. 多不方便啊?是不是,每次生成Framework拖进测试工程,累死了,别急

a.项目中找到你刚才拖入的Framework,show in finder,删除,然后工程中选中红色的Framework,找到如图地方,替换生成Framework的那个路径
b.紧接着Build Setting中设置Search path路径,直接将工程Framework路径拖入输入框,修改删掉部分文字保留到Products/即可
9062174-f2c30c027db6d3cc.png
图1

9062174-64c81b6504fda5b6.png
图2

三、基于Pod自动创建项目

1.github新建仓库


9062174-87f626ea053f45cc.png
github新建仓库

2.Pod自动创建项目

a.终端cd到准备放置pod项目的文件夹中
b.命令-pod lib create XHToolKit(iOS、ObjC、Yes、None、No、XH)
c.关联远程git和本地的pod生成的git
  git remote add origin 远程仓库地址
  git add .
  git commit -m "Pod自动创建项目"
  git push -u origin master
  git pull
d.编译运行一下新程序,给新程序设置一个背景色吧。
e.修改.podspec文件(此处省略具体配置,具体可以去查阅一下)
f.导入封装好的Framework(framework放在Example同级别)
g.测试运行一下吧,有问题要解决哦~

四、万事具备-上传CocoaPods-终端命令

a.将.podspec和Framework推送到github上
   git add .
   git commit -m "Version 0.1.0"
   git push -u origin master
   git tag '0.1.0'
   git push origin --tags
b.验证.podspec文件
   pod spec lint your.podspec --allow-warnings
   pod trunk register yourEmail 'your name' --verbose
   pod trunk me
   pod trunk push your.podspec
9062174-c675f14d7ff1af18.png
成功咯

五、进阶

  1. 你已经成功上传Cocoapods了,可是pod search搜索不到?~/Library/Caches/CocoaPods/search_index.json 将该路径文件删掉就好咯

日常更新~~~

猜你喜欢

转载自blog.csdn.net/weixin_34259159/article/details/87084400