IOS封裝Framework,使用bundle管理資源(二)

上一篇經過 Bundle 建立 framework,這一篇直接經過 iOS 的 Framework來實現,更加簡單。xcode

本篇使用的 Xcode 版本爲 7.2.1 。架構

1、首先,iOS -> Framework&Library -> Cocoa Touch Framework 建立工程。app

工程的名字要與你所指望的 SDK 名字同樣。這裏以 ExpeSDK 做爲項目名。iphone

2、工程建立完成後,刪除自帶的  ExpeSDK.h 和 ExpeSDKTests.m 文件,由於用不到的,移到廢紙簍。函數

3、在 Build Settings 中更改一些配置ui

3.1 Architectures 默認是 armv7和 arm64。能夠增長 armv7s 架構,主要是爲了兼容5C,若是不考慮5C的話,能夠忽略這一步。spa

3.2 更改輸出路徑:在項目根目錄下建立一個build目錄,更改以下配置,輸出文件就會在build文件下,這一步只是爲了方便找到輸出文件而已。code

      Per-configuration Build Products Path > $(SRCROOT)/build/$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME)component

3.3 推薦以下設置:orm

      Mach-O Type > Static Library;( 靜態庫,若是須要提審 Appstore 的話不容許使用動態庫的)

      Dead Code Stripping > NO;(是否消除無效代碼)

      Link With Standard Libraries 默認是YES,若是沒有特殊要求的話,默認就能夠。

                                                設爲 NO 也是能夠打包的,可是使用framework時要配置Other Linker Flags。

      Enable Bitcode > NO;(支持舊庫)

4、將須要共享的文件copy到工程。這裏copy過來的文件是 config.h 和 config.m ;

5、Build Phases -> Headers :

將須要共享的頭文件拖入 Public,  私有的保留在 Project。

6、編譯

模擬器編譯:

Device 編譯:

7、合併二進制文件

編譯成功後,在build目錄下會生成模擬器和設備的framework,合併他們的二進制文件,使framework可以在設備和模擬器上通用;

device的framework:

simulator的framework:

7.1 打開終端,cd 到 build 目錄:

cd build文件路徑

7.2 輸入合成命令:(lipo -create 設備二進制文件 模擬器二進制文件 -output 合成文件的名字)

lipo -create Debug-iphoneos/ExpeSDK.framework/ExpeSDK Debug-iphonesimulator/ExpeSDK.framework/ExpeSDK -output ExpeSDK

合成完畢後,build目錄下會生成一個新的二進制文件:

7.3 替換二進制文件

而後將新的二進制文件替換掉 device framework 中的二進制文件。

移出framework。

OK,framework製做完成!

 


 

不少時候,咱們須要用到圖片,xib等等這些資源,就須要 bundle 來管理。

建立 Bundle 最簡單的方法:新建一個文件,命名爲 xxx.bundle 就OK 了。

此處,我新建一個tuxiang.bundle,而後右擊顯示包內容,放入2張圖片 tx_1.jpg  和 tx_2.png 。

一、新建一個app工程showApp

二、將上面的 ExpeSDK.framework 和 tuxiang.bundle 加入到項目中,如此,當程序運行時,xcode會自動將 tuxiang.bundle 拷貝到 app mainBundle.

  在ExpeSDK.framework的config.m中我已經寫好了獲取tuxiang.bundle中圖片的方法,代碼以下:

- (UIView*)configUI
{
    UIImage * image = [config getPNGImage:@"tx_2"];
    UIImageView * imgView = [[UIImageView alloc] initWithImage:image];
    imgView.frame = CGRectMake(0, 20, 375, 600);
    return imgView;
}
+ (UIImage*)getPNGImage:(NSString*)_image_name
{
    NSBundle * txBundle = [self getBundle:@"tuxiang"];
    NSString * imgPath = [txBundle pathForResource:_image_name ofType:@"png"];
    return [UIImage imageWithContentsOfFile:imgPath];
}
+ (UIImage*)getJPGImage:(NSString*)_image_name
{
    NSBundle * bundle = [self getBundle:@"tuxiang"];
    NSString * imgPath = [bundle pathForResource:_image_name ofType:@"jpg"];
    return [UIImage imageWithContentsOfFile:imgPath];
}

+ (NSBundle*)getBundle:(NSString*)_bundle_name
{
    NSLog(@"mainBundle resourcePath = %@",[NSBundle mainBundle].resourcePath);
    
    /* 從 mainBundle 獲取 tuxiang.bundle */
    // 方法 1
    //    NSString * component = [NSString stringWithFormat:@"%@.bundle",_bundle_name];
    //    NSString * bundlePath = [[NSBundle mainBundle].resourcePath stringByAppendingPathComponent:component];
    
    // 方法 2
    NSString * bundlePath = [[NSBundle mainBundle] pathForResource:_bundle_name ofType:@"bundle"];
    
    NSBundle * bundle = [NSBundle bundleWithPath:bundlePath];
    return bundle;
}

   在config.h文件中提供對外的方法:

- (UIView*)configUI;

三、在 showApp 中調用 configUI:

   先導入頭文件

#import "ExpeSDK/config.h"

   而後在viewDidLoad中調用函數

- (void)viewDidLoad 
{
    [super viewDidLoad];
    
    config * ObjCon = [[config alloc] init];
    [self.view addSubview:[ObjCon configUI]];
}

四、運行:

    

的確是預期的效果。

相關文章
相關標籤/搜索