Path to bundle of iOS framework
Solution 1
Use Bundle(for:Type)
:
let bundle = Bundle(for: type(of: self))
let path = bundle.path(forResource: filename, ofType: type)
or search the bundle by identifier
(the frameworks bundle ID):
let bundle = Bundle(identifier: "com.myframework")
Solution 2
Swift 5
let bundle = Bundle(for: Self.self)
let path = bundle.path(forResource: "filename", ofType: ".plist")
Solution 3
Try below code to get the custom bundle:
let bundlePath = Bundle.main.path(forResource: "CustomBunlde", ofType: "bundle")
let resourceBundle = Bundle.init(path: bundlePath!)
Update
If in your framework, try this:
[[NSBundle bundleForClass:[YourClass class]] URLForResource:@"YourResourceName" withExtension:@".suffixName"];
Solution 4
Simply specify the class name of the resource and below function will give you the Bundle object with which the class is associated with, so if class is associated with a framework it will give bundle of the framework.
let bundle = Bundle(for: <YourClassName>.self)
koen
Updated on July 09, 2022Comments
-
koen almost 2 years
I am working on a framework for iOS, which comes with some datafiles. To load them into a
Dictionary
I do something like this:public func loadPListFromBundle(filename: String, type: String) -> [String : AnyObject]? { guard let bundle = Bundle(for: "com.myframework") let path = bundle.main.path(forResource: filename, ofType: type), let plistDict = NSDictionary(contentsOfFile: path) as? [String : AnyObject] else { print("plist not found") return nil } return plistDict }
If I use this in a playground with the framework, it works as intended.
But if I use the framework embedded in an app, it doesn't work anymore, the "path" now points to the bundle of the app, not of the framework.
How do I make sure that the bundle of the framework is accessed?
EDIT: the code above resides in the framework, not in the app.
EDIT2: the code above is a utility function, and is not part of a struct or class.
-
koen almost 7 yearsThe code to access the bundle is in the framework, not in the app.
-
koen almost 7 yearsWhat is
YourClass
? -
aircraft almost 7 years@Koen test
[self class]
-
aircraft almost 7 years@Koen See this link at 38-line, I exchange with the author long time ago because of the issue: github.com/pikacode/EBForeNotification/blob/master/…
-
koen almost 7 yearsI fail to understand what the
class
is;loadPListFromBundle
is just a utility function (inSwift
, BTW). -
koen almost 7 yearsThe code is not part of a class or struct, so there is no
self
. I have edited my question. -
shallowThought almost 7 yearsUpdated answer.
-
koen almost 7 yearsThanks, using
Bundle(identifier: "com.myframework")
did the trick. -
koen about 5 yearsNot applicable to my question, see the comments to the answer of @aircraft below.
-
Samuel Paul about 5 yearsYourClassName is the class of resource or xib name and is a better way of getting bundle rather than search by bundle identifier.
-
koen about 5 yearsI am loading a plist with lots of data. Please explain how this relates to
YourClassName
-
Samuel Paul about 5 yearsit is related to your question, in you case it's a plist but someone else might want to get the bundle from a resource and might come here to look for the answer.
-
koen about 5 yearsSure, but that's not how SO works. In that case it is better to ask a separate question.
-
Samuel Paul about 5 yearsTake it easy, you don't have to accept this as the answer, as I said it's related to your question and someone else might find it useful, i am only trying to help.
-
koen about 3 yearsPlease add some explanation why you think this is a useful answer.
-
irons163 about 3 years@koen, apparently, It provides 3 kind of most comment usages about how to get the correct bundle, this is very useful especially when you are developing your own framework or using Cocoapods.
-
koen about 3 yearsPlease edit your answer to include your explanations.