iOS開發(fā):iOS SDK 從開發(fā)到發(fā)布

原文地址:http://m.itdecent.cn/p/82d7e467c1d0

iOS SDK 從開發(fā)到發(fā)布

最近在做封裝 SDK(Framework) 的工作,本篇文章將記錄 iOS SDK 從開發(fā)到發(fā)布的具體流程和經(jīng)驗(yàn)總結(jié)。本文主要以圖片形式展示,畢竟有圖才是王道嘛,代碼可在 github鏈接 下載。

首先介紹下創(chuàng)建 SDK 工程以及對應(yīng) Demo 工程的具體流程。

創(chuàng)建 SDK 工程

創(chuàng)建 workspace,用以管理 SDK 和 Demo project

image

創(chuàng)建 Cocoa Touch Framework,并加入到之前創(chuàng)建的 workspace 里

image
image
image

更改工程設(shè)置

  • 更改 Info -> development target 到目標(biāo)系統(tǒng)版本

    image
  • 確認(rèn) Target -> Build Settings -> Mach-O Type 為 Dynamic

    image
  • 更改 Target -> Build Settings -> Build Active Architchture Only 為 NO

    image
  • 更改 Target -> Build Settings -> Bitcode 為 NO

    image
  • 更改 Edit scheme -> Run -> Build Configuration 為 Release

    image

配置公共頭文件

  • 添加測試代碼

    image
  • 設(shè)置測試類的頭文件 .h 為 public

    image
  • 在 MFramework.h 文件中引入所有公開的頭文件

    image

創(chuàng)建 Demo 工程

創(chuàng)建 MDemo project,加入之前創(chuàng)建的 workspace。

image
image
image

更改 MDemo 工程設(shè)置

image
image

[站外圖片上傳中...(image-b6da99-1571883793797)]

集成 SDK 測試

  • 在 Target -> General 配置 Linked Framework 和 Embeded Binaries 為 MFramework

    image
  • 調(diào)用 MFramework 的測試代碼,console 打印如預(yù)期

    image

本地打包 手動發(fā)布

創(chuàng)建 Cross-platform 的 Aggregate,執(zhí)行 build 腳本,通過 lipo 命令將之前構(gòu)建好的 模擬器架構(gòu)的 SDK 產(chǎn)物真機(jī)架構(gòu)的 SDK 產(chǎn)物 合成 適用于真機(jī)和模擬器的 SDK 產(chǎn)物

  • 創(chuàng)建 Cross-platform 的 Aggregate

    image
    image
    image
  • 修改 Configuration, 添加 Dependency 和 Build Script

    image
    image
    image
    image
  • 腳本代碼

    TARGET_NAME=${PROJECT_NAME}
    OUTPUT_DIR=${SRCROOT}/Products/${TARGET_NAME}.framework
    DEVICE_DIR=${BUILD_ROOT}/${CONFIGURATION}-iphoneos/${TARGET_NAME}.framework
    SIMULATOR_DIR=${BUILD_ROOT}/${CONFIGURATION}-iphonesimulator/${TARGET_NAME}.framework
    
    if [ -d "${OUTPUT_DIR}" ]
    then
    rm -rf "${OUTPUT_DIR}"
    fi
    
    mkdir -p "${OUTPUT_DIR}"
    cp -R "${DEVICE_DIR}/" "${OUTPUT_DIR}/"
    
    lipo -create "${DEVICE_DIR}/${TARGET_NAME}" "${SIMULATOR_DIR}/${TARGET_NAME}" -output "${OUTPUT_DIR}/${TARGET_NAME}"
    
    open "${SRCROOT}/Products"
    
    
  • 構(gòu)建 MFramework 的模擬器產(chǎn)物

    image
  • 構(gòu)建 MFramework 的真機(jī)產(chǎn)物

    image
  • 構(gòu)建 MFrameworkCommon,通過 Build Script 會產(chǎn)生 支持模擬器和真機(jī)的 SDK 產(chǎn)物。

    image
    image
  • MDemo 工程集成 SDK 產(chǎn)物 MFramework.framework,添加 Embedded Binaries 和 Linked Frameworks.

    image

持續(xù)構(gòu)建 自動發(fā)布

每次發(fā)布都手動打包,不僅繁瑣,耗人工,而且容易出現(xiàn)遺漏甚至錯誤。下面介紹下如何達(dá)到持續(xù)構(gòu)建和自動發(fā)布。在 workspace 根目錄創(chuàng)建構(gòu)建腳本 build.sh,命令行運(yùn)行腳本 sudo ./build.sh, 則會在 workspace 根目錄下創(chuàng)建 result 文件夾并生成目標(biāo)產(chǎn)物。借助 藍(lán)盾 等持續(xù)構(gòu)建平臺,則可以達(dá)到持續(xù)構(gòu)建,自動發(fā)布,自動歸檔的完美操作。

  • 目標(biāo)產(chǎn)物預(yù)覽

    image
  • 腳本代碼

# 環(huán)境變量
#version=$MajorVersion"."$MinorVersion"."$FixVersion"."$BuildNo
#shortVersion=$MajorVersion"."$MinorVersion"."$FixVersion
version=2.3.4.5
shortVersion=2.3.4

xcworkspace="DevFramework"
scheme="MFramework"
configuration="Release"

WORKSPACE=`pwd`
RESULT_DIR=$WORKSPACE/result

# 清理工作區(qū)
rm -r ~/Library/Developer/Xcode/Archives/`date +%Y-%m-%d`/$scheme\ *.xcarchive
xcodebuild clean -workspace $xcworkspace.xcworkspace -scheme $scheme -configuration $configuration

# 更新版本號
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $version" $scheme/$scheme/Info.plist
/usr/libexec/PlistBuddy -c "Set :CFBundleShortVersionString $shortVersion" $scheme/$scheme/Info.plist

# 分別編譯真機(jī)和模擬器的 framework
xcodebuild -workspace $xcworkspace.xcworkspace -scheme $scheme -configuration $configuration ONLY_ACTIVE_ARCH=NO -sdk iphoneos BUILD_DIR="$RESULT_DIR" BUILD_ROOT="${BUILD_ROOT}" clean build
if ! [ $? = 0 ] ;then
    echo "xcodebuild iphoneos fail"
    exit 1
fi

xcodebuild -workspace $xcworkspace.xcworkspace -scheme $scheme -configuration $configuration ONLY_ACTIVE_ARCH=NO -sdk iphonesimulator BUILD_DIR="$RESULT_DIR" BUILD_ROOT="${BUILD_ROOT}" clean build
if ! [ $? = 0 ] ;then
    echo "xcodebuild iphonesimulator fail"
    exit 1
fi

# 合并 framework,輸出適用真機(jī)和模擬器的 framework 到 result 目錄
cp -R "$RESULT_DIR/${configuration}-iphoneos/${scheme}.framework/" "$RESULT_DIR/${scheme}_${version}.framework/"
lipo -create "$RESULT_DIR/$configuration-iphonesimulator/${scheme}.framework/${scheme}" "$RESULT_DIR/${configuration}-iphoneos/${scheme}.framework/${scheme}" -output "$RESULT_DIR/${scheme}_${version}.framework/${scheme}"
if ! [ $? = 0 ] ;then
    echo "lipo create framework fail"
    exit 1
fi

版本號設(shè)置

Framework 一定要配置版本號的,這樣方便用戶(SDK使用者)接入合適目標(biāo)版本,也有利于后期的定位問題和開發(fā)維護(hù)。
版本號格式推薦是 主版本.特性版本.修正版本.持續(xù)構(gòu)建build號,具體如何配置可以參考上面的【持續(xù)構(gòu)建 自動發(fā)布】。

結(jié)語

看到這里,iOS SDK 開發(fā)到發(fā)布的基本流程都已走通。當(dāng)然 SDK 的開發(fā)工作遠(yuǎn)不止這些,更多的坑和經(jīng)驗(yàn)還要靠各位大佬總結(jié)和分享,hhhh,就先到這里啦~

以下文章可以做一個學(xué)習(xí)參考:
GCD面試要點(diǎn)
block面試要點(diǎn)
Runtime面試要點(diǎn)
RunLoop面試要點(diǎn)
內(nèi)存管理面試要點(diǎn)
MVC、MVVM面試要點(diǎn)
網(wǎng)絡(luò)性能優(yōu)化面試要點(diǎn)
網(wǎng)絡(luò)編程面試要點(diǎn)
KVC&KVO面試要點(diǎn)
數(shù)據(jù)存儲面試要點(diǎn)
混編技術(shù)面試要點(diǎn)
設(shè)計(jì)模式面試要點(diǎn)
UI面試要點(diǎn)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容