Path to Bundle of iOS Framework

Path to bundle of iOS framework

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")

Get a framework's Bundle Identifier programmatically

You can access bundle identifier of an object like below;

let bundle = Bundle(for: AFrameworkClass.self)
let bundleId = bundle.bundleIdentifier

How can I obtain the bundle for my framework code?

I think you're looking for +[NSBundle bundleForClass:].

If you're not inside a class, then you could use +bundleWithIdentifier: (this does mean you need to know the identifier ahead of time). The documentation recommends that method for frameworks that need their own bundle.

iOS Framework - get plist into a dictionary

Use

let path = Bundle(identifier:"com.example.frameworkID")!.path(forResource: "properties", ofType: "plist")!

Why does framework bundle path resolve to DerivedData build directory when targeting Simulator?

When launching from Xcode, Xcode sets DYLD_FRAMEWORK_PATH and DYLD_LIBRARY_PATH to the build products path such that linkage is resolved to your built frameworks when executing tests against them.

This has been the case for quite a long time in Xcode. If you "finger launch" it from SpringBoard, you should get the behavior you are looking for.

bundleForClass not returning framework bundle iOS

You can load NSBundle with Bundle Identifier of framework:

[NSBundle bundleWithIdentifier:YOUR_FRAMEWORK_BUNDLE_IDENTIFIRE];

If the framework has a resources bundle in it, then you can access the resources by:

NSBundle *bundle = [NSBundle bundleForClass:[YOUR_CLASS_NAME class]];
NSURL *url = [bundle URLForResource:RESOURCE_BUNDLE_NAME withExtension:@"bundle"];
NSBundle *resourceBundle = [NSBundle bundleWithURL:url];
UIImage* infoImage = [UIImage imageWithContentsOfFile:[resourceBundle pathForResource:@"info" ofType:@"png"]];

IOS: calling framework’s View Controller. causes 'Could not load NIB in bundle‘ crash

Posting this as an answer, more than happy to delete if it doesn't help.

I don't work with NIB/XIB files, so I'm having difficulty duplicating what you are trying, but I still believe I understand the actual issue. It really doesn't have to do with that, but with bundles, and where you are trying to pull things from.

Here's what I do. For example, let's say you have three things:

  • MyFramework, which contains
  • MyFile, and is being used by
  • MyProject

This seems to me what you want. Now, in your case MyProject imports MyFramework which contains MyFile. BUT...

In MyProject you are trying to access a file that's in the MyFramework bundle:

var loginVC = LoginViewController(nibName: "LoginViewController", bundle: nil)

Here's what works for me. First, I'm accessing something very different - CoreImage kernel files inside MyFramework, instantiating things there as CIFilters. Keep that in mind, but I think it's the same thing you want.

In MyFramework I open the bundle file:

func openBundleFile(_ named:String) -> String {
let myBundle = Bundle.init(identifier: "com.MyCompany.MyFramework")
let kernelPath = (myBundle?.path(forResource: "cikernels", ofType: "bundle"))! + "/" + named + ".cikernel"
do {
return try String(contentsOfFile: kernelPath)
}
catch let error as NSError {
return error.description
}
}

The main thing is the bundle identifier. I don't think this will work inside MyProject for you, but it may.

But I think the best route to go is to:

  • Create an initializer in LoginViewController that will
  • Access the NIB/XIB inside your framework with the appropriate bundle name
  • And remove the `init(nibName:bundle:) from your view controller.

Of course, it my be possible that simply putting in the bundle name in your app will work. :-) But it still feels wrong - frameworks should not require hard-coding of this sort!



Related Topics



Leave a reply



Submit