R.swift icon indicating copy to clipboard operation
R.swift copied to clipboard

[iOS 9] On Demand resource tags support

Open mac-cain13 opened this issue 10 years ago • 2 comments

Think this is something R.swift also could support!

mac-cain13 avatar Aug 06 '15 09:08 mac-cain13

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.

mac-cain13 avatar Nov 30 '15 08:11 mac-cain13

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:

  1. Create a NSBundleResourceRequest with the appropriate resource tags.
  2. Call beginAccessingResources
  3. 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)
}

tomlokhorst avatar Apr 23 '20 12:04 tomlokhorst