Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Path to bundle of iOS framework

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.

like image 613
koen Avatar asked May 20 '17 12:05

koen


People also ask

What is the bundle in iOS?

A bundle is a directory in the file system that groups executable code and related resources such as images and sounds together in one place. In iOS and OS X, applications, frameworks, plug-ins, and other types of software are bundles.

What is iOS framework?

A framework is a bundle (a structured directory) that contains a dynamic shared library along with associated resources, such as nib files, image files, and header files. When you develop an application, your project links to one or more frameworks.

What is a Swift bundle?

SWIFT is a bundle that includes WhatsApp, Facebook, Twitter, Instagram and Snap chat. It includes all these packages and comes with data. Swift bundles are Daily, Weekly and Monthly and cost 1000,2500 and 5000 respectively.

What are iOS modules?

A module is a single unit of code distribution—a framework or application that's built and shipped as a single unit and that can be imported by another module with Swift's import keyword. Each build target (such as an app bundle or framework) in Xcode is treated as a separate module in Swift.


1 Answers

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") 
like image 140
shallowThought Avatar answered Sep 28 '22 09:09

shallowThought