iOS中Cocoapods的Bundle

用Cocoapods做过第三方库的同学应该都知道资源文件(图片、多语言文件等)要放在bundle里面

podspec资源文件写法

  • 需要打Bundle
    s.resources = "Classes/Resources/CTAssetsPickerController.bundle"

s.resources = "你bundle文件的路径"

  • 不需要打Bundle
    spec.resource_bundles = { 'CTAssetsPickerController' => ['CTAssetsPickerController/Resources/CTAssetsPicker.xcassets/*/*.png', 'CTAssetsPickerController/Resources/*.lproj'] }

或者
spec.resource_bundles = { 'CTAssetsPickerController' => ['CTAssetsPickerController/Resources/**/*'] }

**/*这个表示所有文件,包括文件夹

spec.resource_bundles = { '你的库名' => ['资源文件路径/**/*'] }


使用

  • 多语言文件
 extension String {
    var calocale: String {
// 1.创建bundle
        //        let path = Bundle(for: CTAssetsPickerController.self).path(forResource: "CTAssetsPickerController", ofType: "bundle")!

// 这里for:类名可以随便,只要是你库里面的就行。
//  后面.bundle的文件名不能错

// 2.创建bundle
        guard let path = Bundle(for: CTAssetCheckmark.self).path(forResource: "CTAssetsPickerController", ofType: "bundle") else {
            return nil
        }
        let CABundle = Bundle(path: path)

        return NSLocalizedString(self, tableName: "CTAssetsPicker", bundle: CABundle, value: "", comment: "")
    }
}
  • 图片
private var bundleImage: UIImage? {
        guard let path = Bundle(for: CTAssetCheckmark.self).path(forResource: "CTAssetsPickerController", ofType: "bundle") else {
            return nil
        }
        let CABundle = Bundle(path: path)
        return UIImage(named: "你要读取的图片", in:  CABundle, compatibleWith: nil)
    }

参考博客

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容