Path to bundle of iOS framework

27,735

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)
Share:
27,735
koen
Author by

koen

Updated on July 09, 2022

Comments

  • koen
    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
    koen almost 7 years
    The code to access the bundle is in the framework, not in the app.
  • koen
    koen almost 7 years
    What is YourClass ?
  • aircraft
    aircraft almost 7 years
    @Koen test [self class]
  • aircraft
    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
    koen almost 7 years
    I fail to understand what the class is; loadPListFromBundle is just a utility function (in Swift, BTW).
  • koen
    koen almost 7 years
    The code is not part of a class or struct, so there is no self. I have edited my question.
  • shallowThought
    shallowThought almost 7 years
    Updated answer.
  • koen
    koen almost 7 years
    Thanks, using Bundle(identifier: "com.myframework") did the trick.
  • koen
    koen about 5 years
    Not applicable to my question, see the comments to the answer of @aircraft below.
  • Samuel Paul
    Samuel Paul about 5 years
    YourClassName is the class of resource or xib name and is a better way of getting bundle rather than search by bundle identifier.
  • koen
    koen about 5 years
    I am loading a plist with lots of data. Please explain how this relates to YourClassName
  • Samuel Paul
    Samuel Paul about 5 years
    it 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
    koen about 5 years
    Sure, but that's not how SO works. In that case it is better to ask a separate question.
  • Samuel Paul
    Samuel Paul about 5 years
    Take 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
    koen about 3 years
    Please add some explanation why you think this is a useful answer.
  • irons163
    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
    koen about 3 years
    Please edit your answer to include your explanations.