1. 程式人生 > >iOS 比較詳細的自定義framework編譯方法

iOS 比較詳細的自定義framework編譯方法

編譯framework

1、iOS 開發裡面,你不想把原始碼給其他人,你就需要編譯Framework

1 、開啟Xcode建立一個framework工程。

第一步

2、新建一個TestFrame的工程

這裡寫圖片描述

3、新建一個hello的類,並寫兩個方法

這裡寫圖片描述

這裡寫圖片描述

4、修改一些配置資訊,生成支援所有架構的版本。

修改build setting 裡面的build active architecture only 如圖片所示的把Yes修改為NO
這裡寫圖片描述
在build phase 裡面搜尋 “mach”,設定type為 “static library”
這裡寫圖片描述

5、把需要公開的.h檔案公開

在build phase 裡面的headers 裡面的project 右鍵 選擇 move to public group
這裡寫圖片描述

6、使用command +b 編譯 framework

編譯真機 選擇 generic iOS device command +b 編譯
這裡寫圖片描述

編譯模擬器 選擇任意一個模擬器 command +b 編譯
這裡寫圖片描述

編譯成功是這樣的
這裡寫圖片描述

7、在資料夾檢視編譯好的framework

這裡寫圖片描述

8、在資料夾可以看到如圖下面

這裡寫圖片描述

9、把兩個資料夾拷貝到任意一個資料夾下面我這邊是桌面的一個”ytt”資料夾

這裡寫圖片描述

10、下面就需要把模擬器和真機的合併成一個通用版(這裡需要在終端操作)

這些是我的終端操作,下面解釋一下

taijitongdeiMac:~ taiyoshimichi$ cd /Users/taiyoshimichi/Desktop/ytt/Debug-iphoneos
taijitongdeiMac:Debug-iphoneos taiyoshimichi$ ls TestFrame.framework
Headers     Info.plist  Modules     TestFrame
taijitongdeiMac:Debug-iphoneos taiyoshimichi$ lipo -info ./TestFrame.framework/TestFrame
Architectures in
the fat file: ./TestFrame.framework/TestFrame are: armv7 arm64 taijitongdeiMac:Debug-iphoneos taiyoshimichi$ cd .. taijitongdeiMac:ytt taiyoshimichi$ cd /Users/taiyoshimichi/Desktop/ytt/Debug-iphonesimulator taijitongdeiMac:Debug-iphonesimulator taiyoshimichi$ ls TestFrame.framework Headers Info.plist Modules TestFrame _CodeSignature taijitongdeiMac:Debug-iphonesimulator taiyoshimichi$ lipo -info ./TestFrame.framework/TestFrame Architectures in the fat file: ./TestFrame.framework/TestFrame are: i386 x86_64 taijitongdeiMac:Debug-iphonesimulator taiyoshimichi$ cd .. taijitongdeiMac:ytt taiyoshimichi$ lipo -create /Users/taiyoshimichi/Desktop/ytt/Debug-iphoneos/TestFrame.framework/TestFrame /Users/taiyoshimichi/Desktop/ytt/Debug-iphonesimulator/TestFrame.framework/TestFrame -output /Users/taiyoshimichi/Desktop/ytt/Debug-iphoneos/TestFrame.framework/TestFrame taijitongdeiMac:ytt taiyoshimichi$ cd /Users/taiyoshimichi/Desktop/ytt/Debug-iphoneos taijitongdeiMac:Debug-iphoneos taiyoshimichi$ ls TestFrame.framework Headers Info.plist Modules TestFrame taijitongdeiMac:Debug-iphoneos taiyoshimichi$ lipo -info ./TestFrame.framework/TestFrame Architectures in the fat file: ./TestFrame.framework/TestFrame are: armv7 i386 x86_64 arm64 taijitongdeiMac:Debug-iphoneos taiyoshimichi$
第一步、使用 cd 命令到 真機資料夾
cd /Users/taiyoshimichi/Desktop/ytt/Debug-iphoneos
第二步、使用 ls 命令 顯示TestFrame.framework 的所有檔案
ls TestFrame.framework
第三步、使用 lipo -info 檢視真機支援的編碼,可以看到是 armv7, arm64 
lipo -info ./TestFrame.framework/TestFrame
第四步、使用cd .. 回到根路徑
cd .. 
第五步、重複一二三,命令,只不過第一步的真機路徑改為模擬器路徑 最後可以看到編碼是  i386 x86_64

第六步、合併真機和模擬器的framework使用 lipo -create 路徑a  路徑b  -output 路徑a 
cd ..
 lipo -create /Users/taiyoshimichi/Desktop/ytt/Debug-iphoneos/TestFrame.framework/TestFrame /Users/taiyoshimichi/Desktop/ytt/Debug-iphonesimulator/TestFrame.framework/TestFrame -output /Users/taiyoshimichi/Desktop/ytt/Debug-iphoneos/TestFrame.framework/TestFrame
 第七步、cd ..一下重複第一二三步 檢視得到的編碼就是  armv7 i386 x86_64 arm64,說明你編譯成功了

可能出現問題 1、直接合並兩個版本的TestFrame.framework,會便以失敗,因為.framework是不可以合併的,s實際上是合併TestFrame.framework 裡面的TestFrame檔案
可能出現問題2、模擬器版本顯示沒有i386 ,原因:是因為你在修改build setting 裡面的 build active architecture only 沒有把Yes修改為NO,
可能出現的問題3、在使用的時候 執行會報錯 dyld: Library not loaded: ***** Reason: image not found崩潰
解決方法,把framework新增一下如下圖所示

這裡寫圖片描述

再次執行就沒有問題了