自定义组件化Pod资源加载

第一步:添加资源

  • 将资源放到LCCChinaIP/Assets目录文件下,我这里添加的资源为IPSource.bundle
  • 我的工程叫:LCCChinaIP,通过pod lib create LCCChinaIP 会自动生成这样的一个文件夹

第一步:配置podspec

  s.resource_bundles = {
    'LCCChinaIP' => ['LCCChinaIP/Assets/*']
  }

这样的配置会在安装包理自动生成一个LCCChinaIP.bundle 资源包,里面就是你要使用的资源

  • tips: 下面的方法不推荐使用
  s.resource = ['LCCChinaIP/Assets/*']

上面的配置不会生产一个资源包,资源没有分类,会很乱

第三步: 加载资源

    let bundle = Bundle.init(for: LCCChinaIP.self)
    let ipSource = bundle.path(forResource: "LCCChinaIP", ofType: "bundle")
    // tips :
    LCCChinaIP.self 这里的LCCChinaIP 可以是pod 中任何一个类
  • 另外,资源还有其他的加载方式
        var ipSource = Bundle.main.path(forResource: "LCCChinaIP", ofType: "bundle")
        if ipSource == nil {
            ipSource = Bundle.main.path(forResource: "Frameworks/LCCChinaIP.framework/LCCChinaIP", ofType: "bundle")
        }

附录

不同配置下载的文件路径

  • podfile 中配置use_frameworks!
    • podspec使用s.resource_bundles => /Users/xxx/Library/Developer/Xcode/DerivedData/LCCChinaIP-gcohgviddwffgpgelczhvvsbpsvq/Build/Products/Debug-iphonesimulator/LCCChinaIP_Example.app/Frameworks/LCCChinaIP.framework/LCCChinaIP.bundle/IPSource.bundle
    • podspec使用s.resource => /Users/xxx/Library/Developer/Xcode/DerivedData/LCCChinaIP-gcohgviddwffgpgelczhvvsbpsvq/Build/Products/Debug-iphonesimulator/LCCChinaIP_Example.app/Frameworks/LCCChinaIP.framework/IPSource.bundle
  • podfile 中没有配置use_frameworks!
    • podspec使用s.resource_bundles => /Users/xxx/Library/Developer/Xcode/DerivedData/LCCChinaIP-gcohgviddwffgpgelczhvvsbpsvq/Build/Products/Debug-iphonesimulator/LCCChinaIP_Example.app/LCCChinaIP.bundle/IPSource.bundle
    • podspec使用s.resource => /Users/xxx/Library/Developer/Xcode/DerivedData/LCCChinaIP-gcohgviddwffgpgelczhvvsbpsvq/Build/Products/Debug-iphonesimulator/LCCChinaIP_Example.app/IPSource.bundle
    • 可以发现:
      • 使用s.resource_bundles 目录会多一级LCCChinaIP.bundle
      • 使用use_frameworks 目录会多两级Frameworks/LCCChinaIP.framework
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值