How to load resource in cocoapods resource_bundle
I struggled with a similar issue for a while. The resource bundle for a pod is actually a separate bundle from where your code lives. Try the following:
Swift 5
let frameworkBundle = Bundle(for: XDWebViewController.self)
let bundleURL = frameworkBundle.resourceURL?.appendingPathComponent("XDCoreLib.bundle")
let resourceBundle = Bundle(url: bundleURL!)
let image = UIImage(named: "ic_arrow_back", in: resourceBundle, compatibleWith: nil)
print(image)
-- ORIGINAL ANSWER --
let frameworkBundle = NSBundle(forClass: XDWebViewController.self)
let bundleURL = frameworkBundle.resourceURL?.URLByAppendingPathComponent("XDCoreLib.bundle")
let resourceBundle = NSBundle(URL: bundleURL!)
let image = UIImage(named: "ic_arrow_back", inBundle: resourceBundle, compatibleWithTraitCollection: nil)
print(image)
Loading resource_bundle error in cocoapods
Nothing wrong with your code.
Cocoapods on version 1.0.1 supports Assets Catalogs. Please check if yours isn't below it running pod --version
in Terminal.
If a new pod install
does not solved, there is an issue with the xCode New Build System on Assets Catalogs (as mentioned Here) that could be your problem.
You can change the Build System on File
> Workspace Settings...
> Build System
. Try setting it to Legacy Build System
.
Swift 4.2 - Cocoapods: Adding and accessing scnassets in resource_bundles
Suppose ZARKit
is your framework distributed in CocoaPods.
.podspec
includes:
s.resource_bundles = {
'ARUtilsScenes' => ['ARUtils/Assets/ARScenes.scnassets']
}
then you should do this
let bundle = Bundle(for: #Your Class#.self).
let resourcePath = bundle.path(forResource: <#T##String?#>, ofType: <#T##String?#>)
let resourceBundle = Bundle(path: resourcePath)
let url = resourceBundle.url(forResource: "ARScenes.scnassets/<#YourResourceName-arrow#>", withExtension: "<#scn#>")
let arrowScene = SCNScene(url: url)
edit: by Z. Bagley
Accepted answer, but this was the actual code I used:
// find bundle based on existing class in pod
let frameworkBundle = Bundle(for: ARUtilsClass.self) //ARUtilsClass is an empty class, Pod is only extensions for existing classes
// append the scenes bundle, scnassets, and scn/dae
let arrowURL = frameworkBundle.resourceURL?.appendingPathComponent("ARUtilsScenes.bundle/ARScenes.scnassets/newArrow.scn")
// create a node in parent
var arrow = SCNNode()
do {
// get arrow scene from bundle
let bundleArrowScene = try SCNScene(url: (arrowURL)!, options: nil)
// add arrow node from scene
arrow = bundleArrowScene.rootNode.childNode(withName: "arrow", recursively: true)!
} catch {
print("failed to find arrow resource")
}
Related Topics
Hiding Uitabbar When Pushing a Uiview
Disabling Automatic Scrolling of Uitableview When Editing Uitextfield Inside Uitableviewcell
Restore Already Bought In-App-Purchases on Iphone
Add Lefthand Margin to Uitextfield
Crashlytics in iOS Won't Proceed Past "Build Your Project" in Fabric App
Uicollectionview Header Dynamic Height Using Auto Layout
Launch Image Not Showing Up in iOS Application (Using Images.Xcassets)
Nsdateformatter and Current Language in iOS11
How to Prevent Keyboard Push Up Webview at iOS App Using Phonegap
Xcode 4/iOS - Send an Email Using Smtp from Inside My App
When to Use Takeunretainedvalue() or Takeretainedvalue() to Retrieve Unmanaged Objects in Swift
How to Convert an Int to Hex String in Swift
Minimum and Maximum Date in Uidatepicker
Ios: Frame.Size.Width/2 Doesn't Produce a Circle on Every Device
Update the Constant Property of a Constraint Programmatically in Swift