背景:負責
Widget
同事需要研究其它問題,導致前期Widget項目需要找人承接。 代碼熟悉了幾天,就接到需求,需要把widget
獨立封裝成framework
供客戶使用,下面就是抽絲化簡的過程。
一、制作framework
1.在現有項目中新建framework
實現打包
創建framework
- 在項目工程中建framework的好處是不用再為切到哪個項目犯愁,另外一個方便后續打出的widget framework驗證方便。
2.新建完framework
之后的效果如下:
framework效果
3.新建的framework包含PateoWidgetSDK.h
和Info.plist
兩個文件,這時候就需要把需要打包的代碼拖入新建的文件夾中,(如上圖PateoWidgetSDK
文件夾中)。
4.將需要打包代碼中使用到圖片新建bundle用來存儲圖片,不使用Assets.xcassets
,新建之后將bundle添加到Copy Bundle Resources
中,這樣編譯之后的framework中就包含資源bundle了。
新建bundle
5.將需要打包的頭文件添加到Header -->Public中,如果是生成Dynamic Library
庫,這里就需要包含所有的.h
文件(動態庫不支持橋接文件)
暴露頭文件
6.切記如果庫中是OC
、Swift
或SwiftUI
混編的話,.Swift
文件不需要暴露出去,需要在需要暴露的方法或者屬性前聲明Public
,這樣swift的方法或者屬性就可以被外部訪問到。
Swift public 聲明
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}"

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多了一個PateoWidgetSDK-Swift.h
二、使用framework
方式一:本地集成framework
新建target,直接使用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集成后使用如下:
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文件,我就寫死了,還可以寫個方法兼容一下