R.swift
R.swift copied to clipboard
[iOS 9] On Demand resource tags support
Think this is something R.swift also could support!
Started on this earlier and discovered the following:
- Projectfile does contain a resource tags list with all tags, but this list is not kept up to date correctly by Xcode 7
- For images in xcasset folders we should parse the JSON files
- For images/resources outside of a asset folder we should parse the attribute that is in the projectfile, but not from the "all tags"-list, but from the fileref itself.
How we can currently do this:
How to use R.swift with On Demand Resources?
For a full overview of how On Demand Resources work, read Apple's documentation.
With respect to R.swift, it's important to realize that on demand resources don't live in the Main app bundle, but first have to be downloaded. After they've been downloaded, they're available in a different bundle. Also, as of yet R.swift doesn't support static resource tags.
General workflow:
- Create a
NSBundleResourceRequestwith the appropriate resource tags.- Call
beginAccessingResources- In the completion handler, use R to load resources from the request bundle
Example:
let request = NSBundleResourceRequest(tags: ["my-tag"]) request.beginAccessingResources { (error) in guard error != nil else { return } let url = self.request.bundle.url(forResource: R.file.myFile) let image = UIImage(named: R.image.myImage.name, in: request.bundle, compatibleWith: nil) }