iOS组件化之-给自己的组件添加资源文件

在 podspec 中,利用 source_files 可以指定要编译的源代码文件。可是,当我们需要把图片、音频、NIB等资源打包进 Pod 时该怎么办呢?

 1.如何把资源文件打包为.bundle文件?

通常我们都会用.bundle文件把资源文件打包,这里也一样,把你的图片、音频、NIB等资源文件先统一放在一个文件夹里,文件夹名称最好是你的组件名称加上bundle,然后修改文件夹后缀为.bundle,就得到了一个bundle文件;然后把.bundle文件放在与Classes同一级的文件夹下,如下图

 2.如何在podspec文件里配置资源路径?

然后在podspec文件里通过s.resource指定你的资源文件路径,如果你的组件名称叫ICXMeumAssistant,那么你的bundle名称就可以叫ICXIntelligentAssistantBundle.bundle

在podspec文件里配置:

  s.resource     = 'ICXMeumAssistant/ICXIntelligentAssistantBundle.bundle'
 
Pod::Spec.new do |s|
  s.name             = 'ICXMeumAssistant'
  s.version          = '0.1.2'
  s.summary          = '将管家模块代码抽取成组件.'
  s.description      = <<-DESC
TODO: Add long description of the pod here.
                       DESC

  s.homepage         = 'https://git.icarbonx.com/ICX-iOS/ICXMeumAssistant'
  s.license          = { :type => 'MIT', :file => 'LICENSE' }
  s.author           = { ' 吕佳珍' => 'lvjiazhen@icarbonx.com' }
  s.source           = { :git => 'https://git.icarbonx.com/ICX-iOS/ICXMeumAssistant.git', :tag => s.version.to_s }
  s.ios.deployment_target = '8.0'
  s.source_files = 'ICXMeumAssistant/Classes/**/*'
  s.resource     = 'ICXMeumAssistant/ICXIntelligentAssistantBundle.bundle'
  
  s.dependency 'MJExtension'
  s.dependency 'KILabel'
  s.dependency 'MJRefresh'
  s.dependency 'SDWebImage'
  s.dependency 'Masonry'
  s.dependency 'ICXPublicMarcoFile'
  s.dependency 'ICXPublicCategory'
  s.dependency 'ICXPublicTools'
  s.dependency 'ICXMBProgressHUDExtension'
  s.dependency 'TYAudio2WordsManager'
  s.dependency 'ICXUserInfoModule'
  s.dependency 'ICXWebViewModule'
  s.dependency 'ICX10Clock'
end

 

3.如何读取bundle里面的资源?

首先我们需要获取bundle,你是不是立即想到了NSBundle mainBundle ? 但是当你的 pod 库以 framework 形式被使用时,你的资源不是被拷贝到 mainBundle 下,而是被放到 pod 的最终产物—— framework里。此时,你必须保证自己在访问这个 framework 的 bundle,而不是主工程的。

我创建了一个NSBundle的类别文件

先获取framework 的 bundle,ICXMeumAssistantBundleClass是组件里的一个类文件:

+ (NSURL *)ma_AssistantLibraryBundleURL {
    //先获取framework 的 bundle
    NSBundle *bundle = [NSBundle bundleForClass:[ICXMeumAssistantBundleClass class]];
    return [bundle URLForResource:@"ICXIntelligentAssistantBundle" withExtension:@"bundle"];
}

然后再获取我们自己手动创建的bundle:

+ (instancetype) ma_AssistantBundle {
    //再获取我们自己手动创建的bundle
    return [self bundleWithURL:[self ma_AssistantLibraryBundleURL]];
}

 

获取到了我们存放资源文件的bundle文件以后,就可以访问资源文件了,比如获取名为imageName的图片:

+ (UIImage *)ma_AssistantImageNamed:(NSString *)imageName
{
    UIImage *loadingImage = [[UIImage imageWithContentsOfFile:[[self ma_AssistantBundle] pathForResource:[NSString stringWithFormat:@"%@@2x",imageName] ofType:@"png"]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    return loadingImage;
}

加载xib文件需要注意的是当你只是把xib文件放到.bundle文件里的话,那么加载xib文件就会一直报异常

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle </Users/adminuser/Library/Developer/CoreSimulator/Devices/464AB3F8-3C31-4473-AD1B-F554F3F59CDB/data/Containers/Bundle/Application/B4A5F964-A6E0-4357-878F-11A299FD1ACA/MyTestApp.app> (loaded)' with name 'TPDoctorIntroductionView' 

这是因为.bundle文件里只放了xib文件,并没有nib文件,需要进行一下转化:
在终端输入以下命令进行转化:

ibtool --errors --warnings --output-format human-readable-text --compile TPDoctorIntroductionView.nib TPDoctorIntroductionView.xib

如果提示你Xcode路径不对的话,再重置一下你的Xcode路径:

sudo xcode-select -switch /Applications/Xcode9.3.app/Contents/Developer

转化以后,路径下就多了同名的nib文件,但我目前不知道如果有多个xib怎么能通过一句命令全部生成nib

 

然后加载xib文件就不会异常啦

+ (instancetype)doctorView{
 
    return [[NSBundle ma_AssistantBundle] loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil][0];
}  

  加载国际化文件

+ (NSString *)ma_AssistantLocalizedStringForKey:(NSString *)key
{
    return [self ma_AssistantLocalizedStringForKey:key value:nil];
}

+ (NSString *)ma_AssistantLocalizedStringForKey:(NSString *)key value:(NSString *)value
{
    static NSBundle *bundle = nil;
    if (bundle == nil) {
        // (iOS获取的语言字符串比较不稳定)目前框架只处理en、zh-Hans、zh-Hant三种情况,其他按照系统默认处理
        NSString *language = [NSLocale preferredLanguages].firstObject;
        if ([language hasPrefix:@"en"]) {
            language = @"en";
        } else if ([language hasPrefix:@"zh"]) {
            if ([language rangeOfString:@"Hans"].location != NSNotFound) {
                language = @"zh-Hans"; // 简体中文
            } else { // zh-Hant\zh-HK\zh-TW
                language = @"zh-Hant"; // 繁體中文
            }
        } else {
            language = @"en";
        }
        
        // 从.bundle中查找资源
        bundle = [NSBundle bundleWithPath:[[NSBundle ma_AssistantBundle] pathForResource:language ofType:@"lproj"]];
    }
    value = [bundle localizedStringForKey:key value:value table:nil];
    return value;
}

 PS:这里说的只是我个人习惯用的一种方式,其实还可以使用resource_bundles的方式,但道理都是相同的,尽可能避免跟主工程访问冲 

 

 
posted @ 2018-05-18 17:37  记不住密码  阅读(4448)  评论(0编辑  收藏  举报