iOS 打包FrameWork
首先聲明一下,我是一個菜鳥,只是在工作中需要給別的公司提供SDK所以學習了如何去打包生成屬於自己的SDK,有什麽不足的地方,還希望大家能夠指正.
好了,廢話不多說,首先,我使用的Xcode版本是7.2,新建一個工程,在進入頁面之後,選擇Framework & Libray 選項中的 Cocoa Touch Framework.
因為要打包所需的文件,所以這裏我自己創建了CveniEs.h,CveniEs.m文件,並且自定義了一個初始化方法,在,初始化方法中調用了 - (void)getString:(NSString *)string方法,並且打印了一句話,為了證明生成framework之後確實走了該方法;在實際工作過程中,直接把需要打包的所有文件拖到該工程中即可,
然後選中Target,選中Build Phases -> Headers,可以看出有三個選項,分別是Public,Private,Project;把需要暴露給別人的.h文件拖到Public中,剩下的不要暴露的,即為隱藏的文件,放在Project中,
完成上述步驟之後,在littleCveniEs.h文件中,把所有需要暴露的.h文件都用#import <littleCveniEs/PublicHeader.h>引入,記住一定要將所有的需要暴露的.h文件都寫在給定位置,不然編譯後生成的.framework在引用的時候會有警告;
新建一個Target,選擇Other - >Aggregate,重命名,PS:圖中的標記太過於醜陋,可以忽略, - -!
選中新建的Target,點擊Build Pharas ,選中左上角的+,New Run Script Phase;
點開Run Script,添加如下的腳本;
選中Targets中的第一個Target,也就是littleCveniEs,點擊Build Setting -> Mach-O Type,選擇Static Library,到這裏會有一個問題存在,就是無法在iPhone5c上使用.framework,這是我們需要在Build Setting -> Architectures ->Architectures中添加armv7s
我們已經完成了所有的工作,可以選擇Command + B,不過編譯之前,按照下圖選擇正確的Target,編譯完成後會自動彈出已經創建完成的.framework;
到這裏,我們已經完成了百分之九十九的步驟了,導入自己創建的.framework可以實現方法的調用,不過,如果你需要隱藏的文件中含有通過Category創建的類,當你運行程序的時候會發現出現崩潰的現象,如下圖,我在創建自己的.framework的時候隨便寫了一個NSString的Category,並且在初始化方法中調用了Category中的方法,程序直接出現了Unrecognized selector sent to instance 0x10008c098的錯誤,這時我們需要在引用我們.framework的工程中設置 Build Pharse -> Other Linker Flags中加上 -all_load 就OK了,再次運行工程,可以完成所有的打印過程.
作者:CveniEs
鏈接:https://www.jianshu.com/p/305b716df620
腳本代碼:
# 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}"
添加版本號:
想要獲得版本信息就要先設置info.plist文件,在build Settings中設置,當然是在你創建的aggregate中設置
這時的腳本代碼為
# 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. #${SDK_NAME} #SDK_VERSION=$(echo ${SDK_NAME} | grep -o ‘.{3}$‘) #/${DYLIB_CURRENT_VERSION} #/${CURRENT_PROJECT_VERSION} #版本獲取 echo "${PROJECT_DIR}/${INFOPLIST_FILE}" buildNumber=$(/usr/libexec/PlistBuddy -c "Print :CFBundleVersion" "${PROJECT_DIR}/${INFOPLIST_FILE}") buildNumber="$buildNumber" echo "buildNumber = $buildNumber" shortVersion=$(/usr/libexec/PlistBuddy -c "Print :CFBundleShortVersionString" "${PROJECT_DIR}/${INFOPLIST_FILE}") shortVersion="$shortVersion" echo "shortVersion = $shortVersion" #自增build版本 buildVersion=$(echo ${buildNumber} | grep -o -E ‘\d{1,3}$‘) buildVersion=$(($buildVersion + 1)) buildVersion=".$buildVersion" echo "buildVersion = $buildVersion" #設置build版本 newBuildNumber="$shortVersion$buildVersion" echo "newBuildNumber = $newBuildNumber" /usr/libexec/PlistBuddy -c "Set :CFBundleVersion $newBuildNumber" "${PROJECT_DIR}/${INFOPLIST_FILE}" INSTALL_DIR=${SRCROOT}/Products/${buildNumber}/${FMK_NAME}.framework # Working dir will be deleted after the framework creation. WORK_DIR=build DEVICE_DIR=${WORK_DIR}/Release-iphoneos/${FMK_NAME}.framework SIMULATOR_DIR=${WORK_DIR}/Release-iphonesimulator/${FMK_NAME}.framework # -configuration ${CONFIGURATION} "Release" # 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}/" cp -R "${SIMULATOR_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 "${WORK_DIR}" rm -rf "${INSTALL_DIR}/_CodeSignature" rm -rf "${INSTALL_DIR}/Info.plist" open "${INSTALL_DIR}"
iOS 打包FrameWork