1. 程式人生 > >iOS SDK開發入門姿勢詳解

iOS SDK開發入門姿勢詳解


1、建立workspace
兩張圖搞定的事情,就不寫了。
① 開啟Xcode,左上角 File--> New --> Workspace.

② 建立一個資料夾,用來存放我們生成的檔案,成功之後如下。


2、建立SDK
也是,我們幾張圖來搞定
① Xcode左上角 File -> New -> Project.

② 點選 Cocoa Touch Framework.

③ 輸入SDK名字。

④ 選擇group選項;

⑤ 建立完成。



 3、建立Demo
其實都感覺這一部分應該不要的,但是刪了又感覺少了什麼
① 是不是很熟悉;

② 注意下面的group和addto

③ 建立成功之後如下。



 4、Demo關聯SDK
4.1.1 把SDK打包成framework(方法一)
① SDK中建立一個Cat類,等下要用到。


② 把生成的類放到public裡邊,這樣的話才能引用。

③ 注意,接下來的命令列我們用到的是 TGTestSDK.framework 裡邊的 TGTestSDK 。用命令列 `lipo -create (真機路徑) (模擬器路徑) -output (生成檔案路徑)`

④ 然後替換掉.framework檔案下的 TGTestSDK 檔案,這個.framework 檔案就是我們要的。

**4.1.2 把SDK打包成framework(方法二)**
① 方法二要相比方法一更簡單一些,先按照步驟生成一個 File -> New -> Target -> Aggregate 檔案,

② 把下面這段話複製貼上進去,然後`command+B`編譯一下,再把.framework檔案用 `show in Finder` 找到,拖進專案 TGTestDemo 就可以直接用。
注意,格式千萬千萬千萬不要錯,不然會出問題。
注意,編譯的時候千萬選對如圖。

# Sets the target folders and the final framework product.

# 如果工程名稱和Framework的Target名稱不一樣的話,要自定義FMKNAME

# 例如: FMK_NAME = "MyFramework"

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_DIR=${SRCROOT}/Products/${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 -configuration "Release" -target "${FMK_NAME}" -sdk iphoneos clean build

xcodebuild -configuration "Release" -target "${FMK_NAME}" -sdk iphonesimulator clean 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 "${INSTALL_DIR}"

4.2 Demo與SDK關聯
① 把生成的framework拖進專案裡邊,然後就可以使用了


② 這裡可以正常執行,但是報了個警告


③ 這個是需要在 TGTestSDK.h 裡邊把公開的標頭檔案要引用一下,"Missing submodule '********'"的警告就會消失了


 

好了,一個簡單的SDK已經做好,大家可以在此基礎上發揮了,帶有圖片資源的話,把圖片打包成Bundle檔案,和framework檔案一起拷貝到專案中即可。