第一步:添加资源
- 将资源放到
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
- 使用