iOS SDK開發匯總

來源:互聯網
上載者:User

標籤:named   提示   拖拽   control   hid   自己的   target   tor   hone   

以前也做過靜態庫的開發,不過都是一些簡單的調用,最近在做項目的時候,發現其中還有很多問題,所以建個小項目簡單記錄遇到的問題以及正確的解決辦法。在項目中遇到的問題如下:xib檔案擷取不到,            storyboard提示not loadead yet ,            xib和storyboard中的圖片擷取不到。 通常我們進行靜態庫開發的過程包括下面幾個方面:(可以實現在開發靜態庫的同時在主工程中查看代碼結果是否和預期一致)1、主工程:可以實現直接測試靜態庫內的各種模組功能;2、靜態庫:封裝自己的靜態庫,方便使用,使功能模組化;3、bundle資源套件:把靜態庫中的資源檔封裝到bundle包中。資源檔包括圖片、storyboard,xib檔案、plist檔案以及mp3等。4、aggregate:實現指令碼合并靜態庫支援的裝置,產生通用靜態庫。下面開始逐步實現:  一、建立名稱為JFSDKDemo的單控制器工程,設定最低系統版本號碼,設定目標裝置。 二、建立名稱為JFSDKFramework的靜態庫,具體為file—new—target—cocoatouch framework。設定最低系統版本號碼,設定目標裝置         1、設定JFSDKFramework為靜態庫: Build Settings —>Mach-O Type 設定為Static Library           2、建立控制器:SDKVCtest01(勾選xib,同時建立xib檔案)。添加一張圖片備用。然後這個xib中添加兩個UIImageView,其中一個在xib中直接設定圖片,另一個在代碼中設定。xib如下:           3、建立控制器SDKVCtest02,同時建立SDKVCtest02.storyboard,關聯控制器和故事板。SDKVCtest02.storyboard中和上面的xib一樣布局。           4、此時JFSDKFramework的Build Phases如下所示:(可以根據具體情況調整暴露的標頭檔)                    5、此時在ViewController.m中添加兩個控制器的標頭檔。然後分別展示兩個控制器(xib和storyboard),部分代碼如下: 
- (IBAction)pushXib:(UIButton *)sender {       SDKVCtest01 *vc = [[SDKVCtest01 alloc]init];     [self presentViewController:vc animated:YES completion:nil];} - (IBAction)pushStoryboard:(UIButton *)sender {    SDKVCtest02 *vc = [[SDKVCtest02 alloc]init];       [self presentViewController:vc animated:YES completion:nil];}

 

                 發現,啥也沒有,這就對了,因為預設的載入xib,storyboard,圖片等資源是在程式的主bundle中載入的,此時主工程中雖然添加了我們的SDK但是尋找不到相應的資源,因而下面我們對資源檔進行打包,即使用bundle檔案打包資源檔。 三、bundle資源套件:把靜態庫中的資源檔封裝到bundle包中。資源檔包括圖片、storyboard,xib檔案、plist檔案以及mp3等。          產生bundle有兩種方式,                    第一種就是直接在案頭建立檔案夾,修改名稱添加“.bundle”尾碼,然後把資源檔添加進去即可。                    第二種:在Xcode中file—>new —>target —>mac os —>bundle。然後把資源添加到編譯檔案中。            我們採用第二種,       1、因為我們的靜態庫中有xib和storyboard,我們的bundle中需要的是編譯之後的nib和storyboardc檔案,如果採用第一種方式需要每次編譯之後找到編譯的檔案添加到bundle檔案中,這樣很麻煩;       2、此外若xib或者storyboard中直接引用圖片,預設是主bundle,採用第二種方式時,若要修改為自訂bundle只能代碼解決,而第一種方式圖片和xib以及storyboard一起編譯,不用任何其他修改即可完美運行,因而第二種不予採納。         採用第二種的方式只需要添加xib和storyboard檔案到bundle target中,會直接編譯檔案到bundle檔案中。         具體步驟如下:            1、file—>new —>target —>mac os —>bundle 建立bundle命名為“JFSDKSources”。Build Settings中設定Base SDK 為 Latest IOS;Build Settings中設定COMBINE_HIDPI_IMAGES 為NO,防止圖片被編譯為tiff格式;設定最低系統版本號碼,設定目標裝置。            2、把第二步中的xib,storyboard以及圖片資源添加到JFSDKSources中,查看Build Phases確保資源檔被添加到Copy Bundle Resources中(因為Xcode9拖拽的檔案都不會被添加,這個真的很坑,每次都要手動添加),最後如:                3、修改兩個控制器中的代碼引用,把預設的主bundle修改為我們自訂的bundle包:            //SDKVCtest01 
- (instancetype)init{    SDKVCtest01 *testVC =  [[SDKVCtest01 alloc]initWithNibName:@"SDKVCtest01" bundle:[NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:@"JFSDKSources" ofType:@"bundle"]]];    return testVC;}- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view from its nib.    self.view.backgroundColor = [UIColor whiteColor];    self.imageView1.image = [UIImage imageNamed:@"iconjf.png" inBundle:[NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:@"JFSDKSources" ofType:@"bundle"]] compatibleWithTraitCollection:nil];}

 

//SDKVCtest02
- (void)viewDidLoad {    [super viewDidLoad];    self.view.backgroundColor = [UIColor whiteColor];    // Do any additional setup after loading the view.    self.imageView1.image = [UIImage imageNamed:@"iconjf.png" inBundle:[NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:@"JFSDKSources" ofType:@"bundle"]] compatibleWithTraitCollection:nil]; }- (instancetype)init{    SDKVCtest02 *testVC =  [[UIStoryboard storyboardWithName:@"SDKVCtest02" bundle:[NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:@"JFSDKSources" ofType:@"bundle"]]] instantiateViewControllerWithIdentifier:@"SDKVCTest02"];       return testVC;}

 

             4、主工程中添加product中的JFSDKSources.bundle。             command+r,發現xib,storyboard以及png都完美呈現出來,這樣,靜態庫和bundle資源套件基本完成,還需要最後一步。 四、建立aggregate:實現指令碼合并靜態庫支援的裝置,產生通用靜態庫。            1、file—>new —>target —>cross-platform—>aggregate             2、Build Phases 建立Run Script,添加如下指令碼           
FMK_NAME="JFSDKFramework"# Install dir will be the final output to the framework.# The following line create it in the root folder of the current project.SDK_DIR=${SRCROOT}/Products/${FMK_NAME}INSTALL_DIR=${SRCROOT}/Products/${FMK_NAME}/${FMK_NAME}.framework# Working dir will be deleted after the framework creation.WRK_DIR=buildDEVICE_DIR=${WRK_DIR}/Release-iphoneos/${FMK_NAME}.frameworkSIMULATOR_DIR=${WRK_DIR}/Release-iphonesimulator/${FMK_NAME}.framework# -configuration ${CONFIGURATION}# Clean and Building both architectures.xcodebuild -configuration "Release" -target "${FMK_NAME}" -sdk iphoneosxcodebuild -configuration "Release" -target "${FMK_NAME}" -sdk iphonesimulator# Cleaning the oldest.if [ -d "${SDK_DIR}" ]thenrm -rf "${SDK_DIR}"fimkdir -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}"#mv "${INSTALL_DIR}/${FMK_NAME}.bundle" "${SDK_DIR}"open "${SDK_DIR}" 

 

             3、command +R,編譯幾秒鐘彈出framework檔案。 大功告成!!!   附:不同資源在靜態庫中的加 
 //圖片    UIImage *image = [UIImage imageNamed:@"iconjf.png" inBundle:[NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:@"JFSDKSources" ofType:@"bundle"]] compatibleWithTraitCollection:nil];    //storyboard    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"SDKVCtest02" bundle:[NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:@"JFSDKSources" ofType:@"bundle"]]];    //xib    SDKVCtest01 *testVC =  [[SDKVCtest01 alloc]initWithNibName:@"SDKVCtest01" bundle:[NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:@"JFSDKSources" ofType:@"bundle"]]];       NSArray *array = [[NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:@"JFSDKSources" ofType:@"bundle"]] loadNibNamed:@"nibname" owner:nil options:nil];    //mp3    NSString *path = [[NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:@"JFSDKSources" ofType:@"bundle"]] pathForResource:@"test" ofType:@"mp3"];       NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: path];    //plist    NSString *filePath = [[NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:@"JFSDKSources" ofType:@"bundle"]] pathForResource: @"test" ofType: @"plist"];    NSDictionary *plistDic = [NSDictionary dictionaryWithContentsOfFile:filePath]; 

 

  注意:  1、storyboard 在bundle中的檔案應該是編譯之後的 storyboardc 檔案;      2、xib 在bundle中的檔案應該是編譯之後的 nib 檔案;      3、不要讓圖片編譯成tiff格式。  Demo地址:點這裡 參考文檔:靜態庫指令碼合成解釋                   SDK 開發 

iOS SDK開發匯總

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.