背景:负责
Widget
同事需要研究其它问题,导致前期Widget项目需要找人承接。 代码熟悉了几天,就接到需求,需要把widget
独立封装成framework
供客户使用,下面就是抽丝化简的过程。
一、制作framework
1.在现有项目中新建framework
实现打包
- 在项目工程中建framework的好处是不用再为切到哪个项目犯愁,另外一个方便后续打出的widget framework验证方便。
2.新建完framework
之后的效果如下:
3.新建的framework包含PateoWidgetSDK.h
和Info.plist
两个文件,这时候就需要把需要打包的代码拖入新建的文件夹中,(如上图PateoWidgetSDK
文件夹中)。
4.将需要打包代码中使用到图片新建bundle用来存储图片,不使用Assets.xcassets
,新建之后将bundle添加到Copy Bundle Resources
中,这样编译之后的framework中就包含资源bundle了。
5.将需要打包的头文件添加到Header -->Public中,如果是生成Dynamic Library
库,这里就需要包含所有的.h
文件(动态库不支持桥接文件)
6.切记如果库中是OC
、Swift
或SwiftUI
混编的话,.Swift
文件不需要暴露出去,需要在需要暴露的方法或者属性前声明Public
,这样swift的方法或者属性就可以被外部访问到。
7.配置编译环境
a.将Mach-O Type设为Dynamic Library, framework可以是动态库也可以是静态库;
选中PateoWidgetSDK->Build Settings->Mach-O Type(搜索mach)
b.Build Active Architecture Only 修改为NO, 否则生成的库就只支持当前设备的架构;
c.iOS Deployment Target, 库需要支持的最低版本号要小于等于主项目的版本号
d.Architectures 支持的iOS的CPU架构,默认支持arms64和arms7(arms7兼容arm7s),不需要修改。
8.分别选择Any iOS Device
和模拟器编译,然后切换编译环境,Debug和Release再编译一遍,这样就会生成4个包,如果需要生成通用库需要使用终端命令实现,这个过程很繁琐。
9..由于第(6)步很繁琐,可以新建Aggregate
脚本来生成包,具体怎么新建这里就不赘述了,脚本如下:
# Type a script or drag a script file from your workspace to insert its path.
# Sets the target folders and the final framework product.
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_BASE_DIR=${SRCROOT}/Products
INSTALL_DIR=${INSTALL_BASE_DIR}/${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 BITCODE_GENERATION_MODE=bitcode OTHER_CFLAGS="-fembed-bitcode" -arch "arm64" -configuration "Release" -target "${FMK_NAME}" -sdk iphoneos build
xcodebuild BITCODE_GENERATION_MODE=bitcode OTHER_CFLAGS="-fembed-bitcode" -arch "i386" -configuration "Release" -target "${FMK_NAME}" -sdk iphonesimulator build
# Cleaning the oldest.
if [ -d "${INSTALL_DIR}" ]
then
rm -rf "${INSTALL_DIR}"
fi
mkdir -p "${INSTALL_DIR}"
![上传到公司公网仓库中.png](https://upload-images.jianshu.io/upload_images/3346949-2150764d01d386eb.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
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 product dir
open "${INSTALL_BASE_DIR}"
10.还有个重要的点,framework
动态库打包不支持-Bridge
桥接文件,编译生成的framework
中你会发现多了一个PateoWidgetSDK-Swift.h
,这个文件就是编译动态库PateoWidgetSDK.framework
时系统生成的桥接文件。
二、使用framework
方式一:本地集成framework
-这里就能体现在同一个项目中新建framework编译库的方便之处了,可以直接link到Product中的PateoWidgetSDK.framework,这样就省去拖动framework到其他项目的过程,而且还可以做到快速编译,查找问题。
方式二:pod集成framework
-1.新建PateoWidgetSDK.podspec
文件
#
# Be sure to run `pod spec lint PateoWidget.podspec' to ensure this is a
# valid spec and to remove all comments including this before submitting the spec.
#
# To learn more about Podspec attributes see https://guides.cocoapods.org/syntax/podspec.html
# To see working Podspecs in the CocoaPods repo see https://github.com/CocoaPods/Specs/
#
Pod::Spec.new do |spec|
# ――― Spec Metadata ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
#
# These will help people to find your library, and whilst it
# can feel like a chore to fill in it's definitely to your advantage. The
# summary should be tweet-length, and the description more in depth.
#
spec.name = "PateoWidgetSDK"
spec.version = "0.0.1"
spec.summary = "Pateo Widget aodi SDK"
# This description is used to generate tags and improve search results.
# * Think: What does it do? Why did you write it? What is the focus?
# * Try to keep it short, snappy and to the point.
# * Write the description between the DESC delimiters below.
# * Finally, don't worry about the indent, CocoaPods strips it!
spec.description = "Pateo Widget aodi SDK"
spec.homepage = "http://www.xiafanty.com:37990/scm/pateo/pateowidgetsdk"
# spec.screenshots = "www.example.com/screenshots_1.gif", "www.example.com/screenshots_2.gif"
# ――― Spec License ――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
#
# Licensing your code is important. See https://choosealicense.com for more info.
# CocoaPods will detect a license file if there is a named LICENSE*
# Popular ones are 'MIT', 'BSD' and 'Apache License, Version 2.0'.
#
spec.license = "MIT"
# spec.license = { :type => "MIT", :file => "FILE_LICENSE" }
# ――― Author Metadata ――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
#
# Specify the authors of the library, with email addresses. Email addresses
# of the authors are extracted from the SCM log. E.g. $ git log. CocoaPods also
# accepts just a name if you'd rather not provide an email address.
#
# Specify a social_media_url where others can refer to, for example a twitter
# profile URL.
#
spec.author = { "xxx" => "输入邮箱" }
# spec.social_media_url = "https://twitter.com/Simon"
# ――― Platform Specifics ――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
#
# If this Pod runs only on iOS or OS X, then specify the platform and
# the deployment target. You can optionally include the target after the platform.
#
# spec.platform = :ios
# spec.platform = :ios, "9.0"
# When using multiple platforms
# spec.ios.deployment_target = "5.0"
# spec.osx.deployment_target = "10.7"
# spec.watchos.deployment_target = "2.0"
# spec.tvos.deployment_target = "9.0"
# ――― Source Location ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
#
# Specify the location from where the source should be retrieved.
# Supports git, hg, bzr, svn and HTTP.
#
spec.source = { :git => "http://www.xiafanty.com:37990/scm/pateo/pateowidgetsdk.git", :tag => "#{spec.version}" }
# ――― Source Code ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
#
# CocoaPods is smart about how it includes source code. For source files
# giving a folder will include any swift, h, m, mm, c & cpp files.
# For header files it will include any header in the folder.
# Not including the public_header_files will make all headers public.
#
#spec.source_files = "PateoWidgetSDK/**/*.{h,m}"
# spec.exclude_files = "PateoWidgetSDK/Exclude"
# spec.public_header_files = "PateoWidgetSDK/**/*.{h,m}"
# spec.prefix_header_file = 'prefix.pch'
# ――― Resources ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
#
# A list of resources included with the Pod. These are copied into the
# target bundle with a build phase script. Anything else will be cleaned.
# You can preserve files from being cleaned, please don't preserve
# non-essential files like tests, examples and documentation.
#
#spec.resource = "./PateoWidgetSDK.framework/PateoWidgetSDK.bundle"
spec.resource_bundle = { 'ResourceBundle' => ['PateoWidgetSDK.framework/PateoWidgetSDK.bundle'] }
# spec.resources = "Resources/*.png"
# spec.preserve_paths = "FilesToSave", "MoreFilesToSave"
# ――― Project Linking ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
#
# Link your library with frameworks, or libraries. Libraries do not include
# the lib prefix of their name.
#
# spec.framework = "SomeFramework"
# spec.frameworks = "SomeFramework", "AnotherFramework"
# spec.library = "iconv"
# spec.libraries = "iconv", "xml2"
spec.vendored_frameworks = 'PateoWidgetSDK.framework'
# ――― Project Settings ――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
#
# If your library depends on compiler flags you can set them in the xcconfig hash
# where they will only apply to your library. If you depend on other Podspecs
# you can include multiple dependencies to ensure it works.
# spec.requires_arc = true
spec.xcconfig = {
'ENABLE_BITCODE' => 'NO',
'VALID_ARCHS' => 'arm64 arm64e'
}
end
-2.书写PateoWidgetSDK集成.md
集成文档
-3.上传framework到公司公网仓库,这样别人就可以直接通过pod集成
-4.pod集成后使用如下:
注意头文件使用方式,导入的
PateoWidgetSDK
其实就是上面系统生成的PateoWidgetSDK-Swift.h
桥接文件
三、注意事项:
1.bundle资源文件获取,打包生成的framework添加到项目中并没有把bundle文件添加到Copy Bundle Resources
中,这就导致使用[UIBundle mainBundle]获取不到framework
中的PateoWidgetSDK.Bundle
,我使用了很多方法都不能同时兼容本地集成和pod集成,网上也很多方式,最终我使用如下方式实现了,这里仅供参考:
// 读取Bundle中的图片路径
#define QG_IMAGE_PATH(name,type) [[NSBundle bundleWithPath:[[NSBundle bundleForClass:[self class]] pathForResource:@"PateoWidgetSDK" ofType:@"bundle"]] pathForResource:name ofType:type]
//定义UIImage对象 需要显示全称,包括.png
#define QG_IMAGE(imageName) [UIImage imageNamed:QG_IMAGE_PATH(imageName,@"png")]
时间紧促,由于我只使用到png文件,我就写死了,还可以写个方法兼容一下