Bundle extension

import UIKit

/// 定义一个结构体类型的错误类型

struct MyBundleError: Error {

    var errCode: Int = 0

    /// 实现Error协议的localizedDescription只读实例属性

    var localizedDescription: String {

        return "TbundleName和podName不能同时为空 \(errCode)"

    }

}

 

extension Bundle {

    class func bundleWithClass(for ctype:AnyClass,forResourceName resouceName: String, suffixType typeName:String) -> Bundle? {

        return Bundle(path: Bundle(for: ctype).path(forResource: resouceName, ofType: typeName) ?? "")

    }

    class func bundle(withBundleName bundleName: String?, podName: String?) throws -> Bundle? {

        var bpName: String?

        if bundleName == nil && podName == nil {

            throw MyBundleError(errCode: -1)

        } else if bundleName == nil {

            bpName = podName

        } else if podName == nil {

            bpName = bundleName

        }

        var bName: String?

        if bpName?.contains(".bundle") == true {

            bName = bpName?.components(separatedBy: ".bundle").first

        }

        //没使用framwork的情况下

        var associateBundleURL = Bundle.main.url(forResource: bName, withExtension: "bundle")

        //使用framework形式

        if associateBundleURL == nil {

            associateBundleURL = Bundle.main.url(forResource: "Frameworks", withExtension: nil)

            associateBundleURL = associateBundleURL?.appendingPathComponent(bpName ?? "")

            associateBundleURL = associateBundleURL?.appendingPathExtension("framework")

            if let tassociateBundleURL = associateBundleURL, let associateBunle = Bundle(url: tassociateBundleURL){

                associateBundleURL = associateBunle.url(forResource: bundleName, withExtension: "bundle")

            }

        }

        

//        assert((associateBundleURL != nil), "取不到关联bundle")

        //生产环境直接返回空

        

//        return (associateBundleURL != nil) ? Bundle(url: associateBundleURL) : nil

        guard let tlassociateBundleURL = associateBundleURL else {

            return nil

        }

        return  Bundle(url: tlassociateBundleURL)

    }

    

}

 

 

 

 

@implementation NSBundle (AssociatedBundle)
/**
 获取文件所在name,默认情况下podName和bundlename相同,传一个即可
 
 @param bundleName bundle名字,就是在resource_bundles里面的名字
 @param podName pod的名字
 @return bundle
 */
+ (NSBundle *)bundleWithBundleName:(NSString *)bundleName podName:(NSString *)podName{
    if (bundleName == nil && podName == nil) {
        @throw @"bundleName和podName不能同时为空";
    }else if (bundleName == nil ) {
        bundleName = podName;
    }else if (podName == nil) {
        podName = bundleName;
    }
    
    
    if ([bundleName containsString:@".bundle"]) {
        bundleName = [bundleName componentsSeparatedByString:@".bundle"].firstObject;
    }
    //没使用framwork的情况下
    NSURL *associateBundleURL = [[NSBundle mainBundle] URLForResource:bundleName withExtension:@"bundle"];
    //使用framework形式
    if (!associateBundleURL) {
        associateBundleURL = [[NSBundle mainBundle] URLForResource:@"Frameworks" withExtension:nil];
        associateBundleURL = [associateBundleURL URLByAppendingPathComponent:podName];
        associateBundleURL = [associateBundleURL URLByAppendingPathExtension:@"framework"];
        NSBundle *associateBunle = [NSBundle bundleWithURL:associateBundleURL];
        associateBundleURL = [associateBunle URLForResource:bundleName withExtension:@"bundle"];
    }
    
    NSAssert(associateBundleURL, @"取不到关联bundle");
    //生产环境直接返回空
    return associateBundleURL?[NSBundle bundleWithURL:associateBundleURL]:nil;
}
@end



// MARK: 新的获取方式

#import "NSBundle+TLAssociateBundle.h"

 

@implementation NSBundle (TLAssociateBundle)

+ (NSBundle *)bundleWithPathBundleName:(NSString *)bundleName  ResourceName:(NSString *)bundleResourceName{

    // MARK: 新的获取方式

    return [NSBundle bundleWithPath:[[NSBundle bundleForClass:NSClassFromString(bundleName)] pathForResource:bundleResourceName ofType:@"bundle"]];

}

 

 

+ (NSBundle *)bundleWithURLBundleName:(NSString *)bundleName  ResourceName:(NSString *)bundleResourceName {

    NSBundle *bundle = [NSBundle bundleForClass:NSClassFromString(bundleName)];

    NSURL *bundleURL = [bundle URLForResource:bundleResourceName withExtension:@"bundle"];

    return  [self bundleWithURL:bundleURL];

}

@end

 

 


NSBundle *bundle = [NSBundle bundleForClass:[MYSomeClass class]];
NSURL *bundleURL = [bundle URLForResource:bundleNamewithExtension:@"bundle"];
NSBundle *resourceBundle = [NSBundle bundleWithURL: bundleURL];








posted @ 2021-07-21 19:40  sundayswift  阅读(154)  评论(0编辑  收藏  举报