# 做一個自己的 iOS Framework > [time=Fri, May 29, 2020 3:43 PM] ###### tags: `Xcode` `Framework` 約莫一年前因為公司的需求,製作過 `oc` 的 framework;但是沒有做過 `Swift` 的 framework,就讓我捲起袖子用力的把坑給踏平吧! ## 讓我們開始吧 > Xcode 版本 11.5 (11E608c) ### 1. 建立專案  ### 2. 基本設定 建立好專案後先做點基本設定。 * 1. 修改最低系統要求  * 2. Mach-O Type  * 3. Architectures 這裡是一個小坑,需要特別注意。 `Valid Architectures` 要增加 `i386`、`x86_64`,否則無法在模擬器上執行。 * 4. Build Active Architecture Only  > 當設為 NO 時,會編譯所有版本。 > 一般 Debug 模式可以選擇設為 YES,Release 的时候為 NO,以適用不同裝置。 > * 5. Dead Code Stripping 設為 NO 關閉對程式碼中的 `dead`、`unreachable` 程式碼進行過濾。 * 6. Link With Standard Libraries 設為 NO 避免重複連結。 ### 3. 編寫自己的程式碼 在這之前請先嘗試 `command(⌘) + B` 確認是否順利執行。 ### 4. 再次執行 寫完程式碼後 `command(⌘) + B` 發現噴錯了? ``` Ld /Users/CK/Library/Developer/Xcode/DerivedData/MHLogKit-dapqqhqbxveeudcnjhhnvrahekvo/Build/Products/Debug-iphonesimulator/MHLogKit.framework/MHLogKit normal x86_64 (in target 'MHLogKit' from project 'MHLogKit') cd /Users/CK/Documents/Personal/iOS/Swift/Framwork/MHLogKit ``` 解決方式,來到 Xcode: `TARGETS` -> `Build Phases` -> `Link Binary With Libraries` -> `+` -> `libSystem.tbd`  再次 `command(⌘) + B` ,完成。 ### 5. 製作通用的 framework 因為指令集的關係 `實幾` 與 `模擬器` 是不相同的,但打包使用必須使用終端機;科技始終來自人性的慵懶!為了方便日後使用這邊提供一個 `shell script` 讓你在編譯後就完成打包的工作。 來到 Xcode:    `選擇 New Run Script Phase` 將語法貼上,如圖所示:  ``` # Merge Script # 1 # Set bash script to exit immediately if any commands fail. set -e # 2 # Setup some constants for use later on. FRAMEWORK_NAME="Your framework name" # 3 # If remnants from a previous build exist, delete them. if [ -d "${SRCROOT}/build" ]; then rm -rf "${SRCROOT}/build" fi # 4 # Build the framework for device and for simulator (using # all needed architectures). xcodebuild -target "${FRAMEWORK_NAME}" -configuration Release -arch arm64 -arch armv7 -arch armv7s only_active_arch=no defines_module=yes -sdk "iphoneos" xcodebuild -target "${FRAMEWORK_NAME}" -configuration Release -arch x86_64 -arch i386 only_active_arch=no defines_module=yes -sdk "iphonesimulator" # 5 # Remove .framework file if exists on Desktop from previous run. if [ -d "${HOME}/Desktop/${FRAMEWORK_NAME}.framework" ]; then rm -rf "${HOME}/Desktop/${FRAMEWORK_NAME}.framework" fi # 6 # Copy the device version of framework to Desktop. cp -r "${SRCROOT}/build/Release-iphoneos/${FRAMEWORK_NAME}.framework" "${HOME}/Desktop/${FRAMEWORK_NAME}.framework" # 7 # Replace the framework executable within the framework with # a new version created by merging the device and simulator # frameworks' executables with lipo. lipo -create -output "${HOME}/Desktop/${FRAMEWORK_NAME}.framework/${FRAMEWORK_NAME}" "${SRCROOT}/build/Release-iphoneos/${FRAMEWORK_NAME}.framework/${FRAMEWORK_NAME}" "${SRCROOT}/build/Release-iphonesimulator/${FRAMEWORK_NAME}.framework/${FRAMEWORK_NAME}" # 8 # Copy the Swift module mappings for the simulator into the # framework. The device mappings already exist from step 6. cp -r "${SRCROOT}/build/Release-iphonesimulator/${FRAMEWORK_NAME}.framework/Modules/${FRAMEWORK_NAME}.swiftmodule/" "${HOME}/Desktop/${FRAMEWORK_NAME}.framework/Modules/${FRAMEWORK_NAME}.swiftmodule" # 9 # Delete the most recent build. if [ -d "${SRCROOT}/build" ]; then rm -rf "${SRCROOT}/build" fi ``` ### 6. 選擇剛建立好的 target 執行編譯 若編譯成功,打包好的 framework 會出現在 `桌面`,這時候就能將其拉進其他專案內使用了。 --- * 參考 https://www.jianshu.com/p/1ad5bede88bd * 參考 https://www.itread01.com/content/1549898657.html * 參考 https://www.jianshu.com/p/c6eb6e4ebc35 * 參考 https://cloud.tencent.com/developer/ask/80069 * 參考 https://medium.com/@kuotinyen/%E6%89%8B%E6%8A%8A%E6%89%8B%E5%81%9A%E4%B8%80%E5%80%8B%E8%87%AA%E5%B7%B1%E7%9A%84-ios-framework-2aca19dbab95
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up