To load it, we get a reference to its URL inside our app's bundle, then initialize it with Bundle(url:).
The way you access the custom bundle is a little different from the framework because the custom bundle is not loaded by the runtime, so it can't be looked up by identifier. Let image = UIImage(contentsOfFile: imageURL. To access that image from your app, you can instantiate a Bundle using the identifier and then use url(forResource:withExtension:) to get the URL for the image file inside that bundle: guard let bundle = Bundle(identifier: "") else Inside that framework, there's an image file named image.png. Let's say your app has an embedded framework called Utilities and it has the bundle identifier. app extension).Īccessing your app's own resources is cool, but you can also access resources from other bundles. When running on your app, Bundle.main will return your app's bundle (the one that has the. You've probably used the url(forResource:withExtension:) at some point while working on an app, the bundle you call this method on is usually the main bundle, represented by Bundle.main. You can inspect the contents of a bundle in Finder by right-clicking and selecting "Show Package Contents".
Your app is a bundle, every dynamic framework your app is linked to is also a bundle, storyboards are bundles, there are many examples of bundles used in iOS, macOS, tvOS and watchOS.Ī bundle is basically just a folder with some extension, which is displayed by Finder as if it were a file. BundlesĪpple's systems, including iOS, use bundles to represent a collection of resources organized in a directory structure.
Today, I want to talk about what those two things are, what each one of them is supposed to do and how you can use both of them together to create a theming system for an app. Bundles and asset catalogs are features of Apple's systems every developer and app is using, even though many developers are probably not aware of their existence or how powerful they can be, especially when used together.