Use resource bundles in CocoaPods - ios

I am making a pod (MySDK) and would like to load the assets from the separate resource bundles CocoaPods generates.
However, I can not get it to work.
Here is how I tried to load the storyboard:
let storyBoard = UIStoryboard(name: "SDK", bundle: Bundle(identifier:"org.cocoapods.SchedJoulesSDK"))
This gives the error:
'Could not find a storyboard named 'SDK' in bundle
The bundle is added in Xcode:
And my podspec looks like this:
s.resource_bundles = {
'MySDK' => ['SDK/*/*.{xib,storyboard,xcassets}']
}
Any ideas?

If you use resource or resources in a CocoaPods PodSpec file, you tell Cocoapods that these are the resource files your library will load during runtime.
If your library is built as a dynamic framework, these files are just copied to the resource folder path of that framework and everything will be fine. Yet if your library is built as a static library, these are copied to the resource folder of the main application bundle (.app) and this can be a problem as this main application may already have a resource of that name or another Pod may have a resource of that name, in that case these files will overwrite each other. And whether a Pod is built as dynamic framework or as a static library is not specified by the PodSpec but in the Podfile by the application integrating your Pod.
Thus for Pods with resources, it is highly recommended to use resource_bundles instead!
In your case, the lines
s.resource_bundles = {
'MySDK' => ['SDK/*/*.{xib,storyboard,xcassets}'] }
tell CocoaPods to create a resource bundle named MySDK (MySDK.bundle) and place all files matching the pattern into that resource bundle. If your Pod is built as a framework, this bundle is located in the resources folder of your framework bundle; if it is built as a static library, the bundle is copied to the resources folder of the main application bundle, which should be safe if you name your bundle the same way as your Pod (you should not name it "MySDK", rather "SchedJoulesSDK").
This bundle will have the same identifier as your Pod, however when dynamic frameworks are built, your framework bundle will have that identifier as well and then it's undefined behavior which bundle is being loaded when you load it by identifier (and currently the outer bundle always wins in my tests).
Correct code would look like this (not tested, though):
// Get the bundle containing the binary with the current class.
// If frameworks are used, this is the frameworks bundle (.framework),
// if static libraries are used, this is the main app bundle (.app).
let myBundle = Bundle(for: Self.self)
// Get the URL to the resource bundle within the bundle
// of the current class.
guard let resourceBundleURL = myBundle.url(
forResource: "MySDK", withExtension: "bundle")
else { fatalError("MySDK.bundle not found!") }
// Create a bundle object for the bundle found at that URL.
guard let resourceBundle = Bundle(url: resourceBundleURL)
else { fatalError("Cannot access MySDK.bundle!") }
// Load your resources from this bundle.
let storyBoard = UIStoryboard(name: "SDK", bundle: resourceBundle)
As resourceBundle cannot change at runtime, it is safe to create it only once (e.g. on app start or when your framework is initialized) and store it into a global variable (or global class property), so you have it always around when needed (a bundle object also hardly uses any RAM memory, as it only encapsulates meta data):
final class SchedJoulesSDK {
static let resourceBundle: Bundle = {
let myBundle = Bundle(for: SchedJoulesSDK.self)
guard let resourceBundleURL = myBundle.url(
forResource: "MySDK", withExtension: "bundle")
else { fatalError("MySDK.bundle not found!") }
guard let resourceBundle = Bundle(url: resourceBundleURL)
else { fatalError("Cannot access MySDK.bundle!") }
return resourceBundle
}()
}
The property is initialized lazy (that's default for static let properties, no need for the lazy keyword) and the system ensures that this happen only once, as a let property must not changed once initialized. Note that you cannot use Self.self in that context, you need to use the actual class name.
In your code you can now just use that bundle wherever needed:
let storyBoard = UIStoryboard(name: "SDK",
bundle: SchedJoulesSDK.resourceBundle)

You can use like...
s.resource = "icon.png" //for single file
or
s.resources = "Resources/*.png" //for png file
or
s.resources = "Resources/**/*.{png,storyboard}" //for storyboard and png files
or
s.resource_bundles = {
"<ResourceBundleName>" => ["path/to/resources/*/**"]
}

This is what I ended up with...
import Foundation
public extension Bundle {
public static func resourceBundle(for frameworkClass: AnyClass) -> Bundle {
guard let moduleName = String(reflecting: frameworkClass).components(separatedBy: ".").first else {
fatalError("Couldn't determine module name from class \(frameworkClass)")
}
let frameworkBundle = Bundle(for: frameworkClass)
guard let resourceBundleURL = frameworkBundle.url(forResource: moduleName, withExtension: "bundle"),
let resourceBundle = Bundle(url: resourceBundleURL) else {
fatalError("\(moduleName).bundle not found in \(frameworkBundle)")
}
return resourceBundle
}
}
Example usage...
class MyViewController: UIViewController {
init() {
super.init(nibName: nil, bundle: Bundle.resourceBundle(for: Self.self))
}
}
or
tableView.register(UINib(nibName: Self.loginStatusCellID, bundle: Bundle.resourceBundle(for: Self.self)), forCellReuseIdentifier: Self.loginStatusCellID)

I have the same issue, if I only use the bundle line:
s.resource_bundles = {
'MySDK' => ['SDK/*/*.{xib,storyboard,xcassets}']
}
I get a runtime crash when I reference my storyboard. However, if I explicitly add each storyboard as a resource like so:
s.resources = ["Resources/MyStoryboard.storyboard", "Resources/MyStoryboard2.storyboard"]
Everything works fine. I don't think we should have to explicitly add each storyboard as a resource, but I haven't been able to make it work any other way.
One thing you could try is changing you resource bundle reference to recursively search in your folders with the ** nomenclature like this:
s.resource_bundles = {
'MySDK' => ['SDK/**/*.{xib,storyboard,xcassets}']
}

Related

Using fonts from xcfamework

So I have xcframework which have fonts file (from react-native-vector-icons).
In test project (testApp), I am importing xcframework and I was expecting it to copy resources from my framework but unfortunately it doesn't copy these fonts which are there inside the xcframework and I have to manually add it in Copy Bundle Resources.
For end user, I don't want them to go inside framework and add manually.
In my framework, I have Cops Pods resources in Build Phase but haven't done anything such which would tell the project consuming this framework to copy these fonts (or resources).
What do I need to do so that in project it copies fonts (or resources) by itself? instead of manually doing it?
If you distribute your xcframework using Cocoapods then you can have any resource file automatically copied in the destination .app bundle by Cocoapods.
In your podspec you define the resources you need to copy and the xcframework (and NO spec.sources):
spec.resources = ['Fonts/EncodeSans-ExtraBold.ttf']
spec.vendored_frameworks = 'Example.xcframework'
Or from within the xcframework:
spec.resources = ['Example.xcframework/<a path>/EncodeSans-ExtraBold.ttf']
spec.vendored_frameworks = 'Example.xcframework'
When you execute pod install Cocoapods will create a "[CP] Copy Pods Resources" phase that will copy in the destination app whatever you have defined in the resources:
To avoid collisions(the user or other pod might copy the same file), better use resource_bundles instead of resources.
You need to register the fonts with the font manager. I did this extending UIFont
extension UIFont {
private class ModuleClass {}
static func fontsURLs() -> [URL] {
let bundle = Bundle(for: ModuleClass.self)
let fileNames = ["Feather"]
return fileNames.compactMap { fontName in
bundle.url(forResource: fontName, withExtension: "ttf")
}
}
/// Registers the specified graphics font with the font manager.
/// - Parameter url: Bundle URL of the font to register
/// - Throws: If font cannot be registered
static func register(from url: URL) throws {
guard let fontDataProvider = CGDataProvider(url: url as CFURL) else {
throw FontError.dataProviderNotCreated(message: "Could not create font data provider for \(url).")
}
guard let font = CGFont(fontDataProvider) else {
throw FontError.fontNotCreated(message: "Could not create font data provider for \(url).")
}
var error: Unmanaged<CFError>?
guard CTFontManagerRegisterGraphicsFont(font, &error) else {
throw error?.takeUnretainedValue() ?? FontError.fontNotRegistered
}
}
}
Then, you need use this.
public extension UIFont {
/// Registers the specified graphics font with the font manager.
/// Registered fonts participate in font descriptor matching.
static func registerFonts() {
let urls = fontsURLs()
for url in urls {
do {
try register(from: url)
} catch {
debugPrint(error)
}
}
}
}
and in your main project (app) register the fonts
// In AppDelegate.swift
import YourModule
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
UIFont.registerFonts()
return true
}

Localizable.strings from framework is not getting inside framework

I have a project with some basic functionalities which I can reuse and I am trying to use that as a framework in another project. So my podspec includes strings, swift, assets files and all got added in to the project but the strings keys are not taking the value from localizables.strings. Basically it is not getting from the bundle path. Here is the code where I am calling the strings file inside the framework. "path" is getting as nil. It is working fine in the base project
let bundle: Bundle = .main
if let currentLanguage = languageManager.getCurrentLanguage() {
if let path = bundle.path(forResource: currentLanguage.rawValue, ofType: "lproj"),
let bundle = Bundle(path: path) {
return bundle.localizedString(forKey: self, value: nil, table: nil)
}
}
I fixed the issue.. In podspec, it was
spec.resource_bundle = { 'AppName' => ['AppName/**/*.{strings}'] }
I changed it to
spec.resources = "AppName/**/*.{xcassets}", "AppName/**/*.{Strings}"

Get all URLs for resources in sub-directory in Swift

I am attempting to create an array of URLs for all of the resources in a sub-directory in my iOS app. I can not seem to get to the correct path, I want to be able to retrieve the URLs even if I do not know the names (i.e. I don't want to hard code the file names into the code).
Below is a screen shot of the hierarchy, I am attempting to get all the files in the 'test' folder:
Any help is greatly appreciated, I have attempted to use file manager and bundle main path but to no joy so far.
This is the only code I have currently:
let fileManager = FileManager.default
let path = Bundle.main.urls(forResourcesWithExtension: "pdf", subdirectory: "Files/test")
print(path)
I have also tried this code but this prints all resources, I can't seem to specify a sub-directory:
let fm = FileManager.default
let path = Bundle.main.resourcePath!
do {
let items = try fm.contentsOfDirectory(atPath: path)
for item in items {
print("Found \(item)")
}
} catch {
// failed to read directory – bad permissions, perhaps?
}
Based on an answer from #vadian , The folders were changed from virtual groups to real folders. Using the following code I was able to get a list of resources:
let fileManager = FileManager.default
let path = Bundle.main.resourcePath
let enumerator:FileManager.DirectoryEnumerator = fileManager.enumerator(atPath: "\(path!)/Files/test")!
while let element = enumerator.nextObject() as? String {
if element.hasSuffix("pdf") || element.hasSuffix("jpg") { // checks the extension
print(element)
}
}
Consider that the yellow folders are virtual groups, not real folders (although Xcode creates real folders in the project directory). All files in the yellow folders are moved into the Resources directory in the bundle when the app is built.
Real folders in the bundle are in the project navigator.
You can follow the following steps to get them:
Create a new folder inside your project folder with the extension is .bundle (for example: Images.bundle).
Copy resource files into that new folder.
Drag that new folder into the project that opening in Xcode.
Retrieve the URLs by using the following code snippet:
let urls = Bundle.main.urls(forResourcesWithExtension: nil, subdirectory: "Images.bundle")
You can also view the guide video here: https://youtu.be/SpMaZp0ReEo
I came across a similar issue today. I needed to retrieve the URL of a resource file in a bundle ignoring its path.
I wrote the following:
public extension Bundle {
/// Returns the file URL for the resource identified by the specified name, searching all bundle resources.
/// - Parameter resource: The name of the resource file, including the extension.
/// - Returns: The file URL for the resource file or nil if the file could not be located.
func recurseUrl(forResource resource: String) -> URL? {
let contents = FileManager.default.allContentsOfDirectory(atPath: self.bundlePath)
for content in contents {
let fileNameWithPath = NSString(string: content)
if let fileName = fileNameWithPath.pathComponents.last {
if resource == fileName {
return URL(fileURLWithPath: content)
}
}
}
return nil
}
Based on this:
public extension FileManager {
/// Performs a deep search of the specified directory and returns the paths of any contained items.
/// - Parameter path: The path to the directory whose contents you want to enumerate.
/// - Returns: An array of String objects, each of which identifies a file or symbolic link contained in path. Returns an empty array if the directory does not exists or has no contents.
func allContentsOfDirectory(atPath path: String) -> [String] {
var paths = [String]()
do {
let url = URL(fileURLWithPath: path)
let contents = try FileManager.default.contentsOfDirectory(atPath: path)
for content in contents {
let contentUrl = url.appendingPathComponent(content)
if contentUrl.hasDirectoryPath {
paths.append(contentsOf: allContentsOfDirectory(atPath: contentUrl.path))
}
else {
paths.append(contentUrl.path)
}
}
}
catch {}
return paths
}
}
Which achieves the goal of retrieving the URL for the first match of a given resource filename in a bundle's resources, all directories wide.
I tend to think that Swift's func url(forResource name: String?, withExtension ext: String?) -> URL? should behave this way in the first place.

How to load resource in cocoapods resource_bundle

I have struggled a lot to how to load resource in cocoapods resource_bundle.
The following is what i put in the .podspecs file.
s.source_files = 'XDCoreLib/Pod/Classes/**/*'
s.resource_bundles = {
'XDCoreLib' => ['XDCoreLib/Pod/Resources/**/*.{png,storyboard}']
}
This is what I am trying to do from the main project.
let bundle = NSBundle(forClass: XDWebViewController.self)
let image = UIImage(named: "ic_arrow_back", inBundle: bundle, compatibleWithTraitCollection: nil)
print(image)
I did see the picture in the XDCoreLib.bundle, but it return a nil.
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)
I don't think any of the other answers have described the relationship to the Podspec. The bundle URL uses the name of the pod and then .bundle to find the resource bundle. This approach works with asset catalogs for accessing pod images both inside and outside of the pod.
Podspec
Pod::Spec.new do |s|
s.name = 'PodName'
s.resource_bundles = {
'ResourceBundleName' => ['path/to/resources/*/**']
}
end
Objective-C
// grab bundle using `Class` in pod source (`self`, in this case)
NSBundle *bundle = [NSBundle bundleForClass:self.classForCoder];
NSURL *bundleURL = [[bundle resourceURL] URLByAppendingPathComponent:#"PodName.bundle"];
NSBundle *resourceBundle = [NSBundle bundleWithURL:bundleURL];
UIImage *podImage = [UIImage imageNamed:#"image_name" inBundle:resourceBundle compatibleWithTraitCollection:nil];
Swift
See this answer.
This will work
In .podspec add s.resources in addition to s.resource_bundles (pod install afterwards)
s.resources = 'XDCoreLib/Pod/Resources/**/*.{png,storyboard}'
Then in your code, its as easy as:
let image = UIImage(named: "image_name.png", in: Bundle(for: type(of: self)), compatibleWith: nil)
Note: You may need to run pod install after updating the .podspec to have the changes to your Xcode proj
You can just check the ID of the Bundle by selecting the Pods project, selecting the desired target, and making sure you're on the General tab.
Then in code you can load say an image in that Pod like so:
backgroundImageView.image = UIImage(named: "business_fellas", in: Bundle(identifier: "org.cocoapods.StandardLibrary3-0"), compatibleWith: nil)
Can create an extension to access the framework bundle easier in your framework source code
extension Bundle {
static func getResourcesBundle() -> Bundle? {
let bundle = Bundle(for {your class}.self)
guard let resourcesBundleUrl = bundle.resourceURL?.appendingPathComponent({bundle name}) else {
return nil
}
return Bundle(url: resourcesBundleUrl)
}
}
Just for the record: I wanted to open a NIB from a Category stored in a CocoaPods library. Of course [self classForCoder] gave UIKit back (because of the Category), so the above methods didn't work.
I resolved the issue using some hardcoded paths:
NSURL *bundleURL = [[[NSBundle mainBundle] resourceURL] URLByAppendingPathComponent:#"Frameworks/MyCocoaPodLibraryName.framework"];
NSBundle *podBundle = [NSBundle bundleWithURL:bundleURL];
CustomView *customView = [[podBundle loadNibNamed:#"CustomView" owner:self options:nil] firstObject];
It seems like for some reason **/*.{extensions} pattern is required for this to work I ended up creating my podspec like this
s.resource_bundle = { '<BundleName>' => 'Pod/Resources/**/*.storyboard' }
s.resource = 'Pod/Resources/**/*.storyboard'
even though my actual path is Pod/Resources/.storyboard*
And then to find my bundle I used
#Jhelzer's answer although any way to get bundle e.g. by class or URL or bundleID will work after above setup.
You can also have a look into this answer for more refrences.
Interesting thing is when you need to do it in the source files that are also in the cocoapod library
I faced it when used a xib and plist files in my pod.
That was solved in the next way. Example of loading a xib file:
let bundle = Bundle(for: self.classForCoder)
let viewController = CocoapodViewController(nibName: "CocoapodViewController", bundle: bundle)
import class Foundation.Bundle
private class BundleFinder {}
extension Foundation.Bundle {
/// Returns the resource bundle associated with the current Swift module.
static var module: Bundle = {
let bundleName = "ID3TagEditor_ID3TagEditorTests"
let candidates = [
// Bundle should be present here when the package is linked into an App.
Bundle.main.resourceURL,
// Bundle should be present here when the package is linked into a framework.
Bundle(for: BundleFinder.self).resourceURL,
// For command-line tools.
Bundle.main.bundleURL,
]
for candidate in candidates {
let bundlePath = candidate?.appendingPathComponent(bundleName + ".bundle")
if let bundle = bundlePath.flatMap(Bundle.init(url:)) {
return bundle
}
}
fatalError("unable to find bundle named ID3TagEditor_ID3TagEditorTests")
}()
}
From: https://www.fabrizioduroni.it/2020/10/19/swift-package-manager-resources/
For me the following worked because I'm using a static pod.
s.resource_bundle = '<BundleName>' => 'Path to resources'
s.resource = 'Path to resources'

Get file path from a NSBundle which is not the mainBundle

I'm trying to get a pathForResource for a plist file, when the file was in the mainBundle it was easy, i used:
let path = NSBundle.mainBundle().pathForResource("Settings", ofType: "plist")
But now i moved the settings file to another bundle and i don't know how to get it. I tried to use forClass and allBundles but i'm a swift rookie and didn't managed to make it work. Also coulden't find the solution over the web. It seems all the NSBundle usages examples is only for mainBundle
If you don't know which bundle has your resource you could loop through all of them:
let bundles = NSBundle.allBundles()
var path : String?
var myBundle : NSBundle?
for bundle in bundles {
if let resourcePath = (bundle as! NSBundle).pathForResource("Settings", ofType: "plist") {
path = resourcePath
myBundle = bundle
break
}
}
On the other hand, if you do have the path or the identifier of your bundle you could use:
let bundle = NSBundle(path: "the-bundle-path")
or
let bundle = NSBundle(identifier: "bundle-identifier")
This has nothing to do with the level of your Swift programming knowledge, rather with the interface NSBundle exposes. You should check the docs out to see if they can help you.

Resources