Try   HackMD

做一個自己的 iOS Framework

Fri, May 29, 2020 3:43 PM

tags: Xcode Framework

約莫一年前因為公司的需求,製作過 oc 的 framework;但是沒有做過 Swift 的 framework,就讓我捲起袖子用力的把坑給踏平吧!

讓我們開始吧

Xcode 版本 11.5 (11E608c)

1. 建立專案

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

2. 基本設定

建立好專案後先做點基本設定。

    1. 修改最低系統要求
      Image Not Showing Possible Reasons
      • The image file may be corrupted
      • The server hosting the image is unavailable
      • The image path is incorrect
      • The image format is not supported
      Learn More →
    1. Mach-O Type
      Image Not Showing Possible Reasons
      • The image file may be corrupted
      • The server hosting the image is unavailable
      • The image path is incorrect
      • The image format is not supported
      Learn More →
    1. Architectures
      這裡是一個小坑,需要特別注意。
      Valid Architectures 要增加 i386x86_64,否則無法在模擬器上執行。
    1. Build Active Architecture Only
      Image Not Showing Possible Reasons
      • The image file may be corrupted
      • The server hosting the image is unavailable
      • The image path is incorrect
      • The image format is not supported
      Learn More →

當設為 NO 時,會編譯所有版本。
一般 Debug 模式可以選擇設為 YES,Release 的时候為 NO,以適用不同裝置。

    1. Dead Code Stripping
      設為 NO 關閉對程式碼中的 deadunreachable 程式碼進行過濾。
    1. 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

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

再次 command(⌘) + B ,完成。

5. 製作通用的 framework

因為指令集的關係 實幾模擬器 是不相同的,但打包使用必須使用終端機;科技始終來自人性的慵懶!為了方便日後使用這邊提供一個 shell script 讓你在編譯後就完成打包的工作。

來到 Xcode:

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

選擇 New Run Script Phase 將語法貼上,如圖所示:

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

# 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 會出現在 桌面,這時候就能將其拉進其他專案內使用了。