Loading nib from my own bundle? - objective-c

i'm trying to put NIB/XIB files in a bundle I call Configuration.bundle. When I try to load the xib from my bundle the app crashes because it can't find the xib file.
NSBundle *bundle = [NSBundle bundleWithPath:[[NSBundle mainBundle]
pathForResource:#"Configuration" ofType:#"bundle"]];
[bundle load];
NSLog(#"bundle: %#", bundle);
I get the output
bundle: NSBundle (not yet loaded)
The 'not yet loaded' part scares me a bit. Why isn't it loaded?
And finally when I try to load my nib with the view controller
ConfigViewController *configViewController = [[ConfigViewController alloc] initWithNibName:#"ConfigViewController" bundle:bundle];
I get the output
* Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle (not yet loaded)' with name 'ConfigViewController.xib'
I've tried both with and without .xib.
Any ideas?

The load method is used to load executable code like frameworks and these things.
Your bundle doesn't contain any executable file that you want to load, so you don't neel to call [bundle load].
If the name of the bundle ir right, then all you've written except for [bundle load] is fine.Anyways check that the path is the right ones, don't nest too much the instructions:
NSString* path=[ [NSBundle mainBundle] pathForResource: #"Configuration" ofType: #"bundle"];
If this string is the right path, then you are sure that the bundle will be loaded correctly without calling [bundle load].

First of all if a bundle is not created properly it will not get loaded. So in-order to create a proper bundle below are the steps for creating a bundle:
1. Add a new target by choosing a templete named bundle under OS X -> Framework & Libraries.
Select newly created target and change BaseSDK from OSX to Latest iOS.
Add .xibs, images or other resources which you want to use it from bundle in Build Phrases -> Copy Bundle Resources.
Add CoreFoundation framework in Build Phrases -> Link binary with Libraries.
Compile the target choosing iOS Device.
Save the newly created bundle from Products directory to some place.
Now copy that bundle into your main project. Load a bundle using below code:
NSString *path = [[NSBundle mainBundle] pathForResource:#"BundleName" ofType:#"bundle"];
NSBundle *bundle = [NSBundle bundleWithPath:path];"
You are now set with the new bundle.

Related

iOS Webview loadRequest from string returns nil on ipad and ipad retina

I've a menu on my app which loads local html file but it detects first if there is an updated file inside documents directory and if the file exists, the value of the html_path will load the updated file with its path on docs.dir of the app. This code works fine with iphone, and ipad retina 64 bit, but on ipad and ipad retina, the html_path variable returns nil.
NSString *html_path;
html_path = [[NSBundle mainBundle]
pathForResource:#"somehtml"
ofType:#"html" inDirectory:#"www"];
local_request = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:html_path]];
[_webView loadRequest:local_request];
Below is the error that I got:
Terminating app due to uncaught exception `NSInvalidArgumentException`,
reason: `*** -[NSURL initFileURLWithPath:]: nil string parameter`
I wonder the cause of this error, please help as I'm stuck with this error.
I suspect this could be a problem with your resources not being included in the bundle for the application.
Select your target, go to "Build Phases" > "Copy Bundle Resources", press the "+" button and add your "somehtml.html" to the bundle.
I have solved the issue, it seems like
html_path = [[NSBundle mainBundle]
pathForResource:#"somehtml"
ofType:#"html" inDirectory:#"www"];
that code above does not return the correct path, or the correct string path to be recognized by the NSURL fileURLWithPath.
the solution that i came up with was, i separated the bundle path and put that on the string
NSString *bundle = [[NSBundle mainBundle] bundlePath];
then i use stringByAppendingPathComponent to concatenate the html file name
htmlFile = [bundle stringByAppendingPathComponent:#"www/somehtml.html"];

Get pdf files from Bundle

I created the Settings Bundle
I done it. Because i need serf inside the bundle as the folder. For Example FileManager.
Below you can see.
And I opened meeting.Bundle in Finder using show contents and put into the Folders with pdf files. I try get these pdf files it on the device (iPhone 5) with code
NSString *bundlePath = [[NSBundle mainBundle] pathForResource:#"meeting" ofType:#"bundle"];
NSString *filePathPdf = [[NSBundle bundleWithPath:bundlePath] pathForResource:theFileName ofType:#"pdf"];
But *filePathPdf is nil. Xcode cannot find them.
What i do wrong?
PS: I found the way how to load pdf files but i think this is wrong method.
I put into meeting.bundle the folder with pdf files and put the same folder into main bundle.
Load with path:
NSString *fileNamePdf = [self.contents objectAtIndex:indexPath.row];
NSString* theFileName = [[fileNamePdf lastPathComponent] stringByDeletingPathExtension];
NSString *filePathPdf = [[NSBundle mainBundle] pathForResource:theFileName ofType:#"pdf"];
Pdf Files loading from main bundle. But references of pdf files are taken from meeting.bundle
Maybe somebody knows how to make the right to work?

How to load resources when using cocoapods resource_bundles and testing in a different bundle

im using cocoapods for managing a private library im working on. Currently i have a subspec with all my models, including the .xcdatamodel file for core data modeling. My podspec file looks like this.
s.subspec 'Data' do |ds|
...
ds.resource_bundle = { :DataKit => '/Data/Schema/App.xcdatamodeld' }
...
end
When i need to access my .xcdatamodel y look up for my DataKit bundle.
For example
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/DataKit.bundle",[[NSBundle mainBundle] resourcePath]]];
NSBundle *dataBundle = [NSBundle bundleWithURL:url];
NSManagedObjectModel *managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:#[dataBundle]];
The problem i'm having is the i need to test some code in the Data subspec, so, when i run the tests there is no DataKit bundle. I wouldn't like to add conditional code for loading the bundle depending if im testing or not. Any good solution for this?

Error implementing UIWebView to load local html

am trying to add a UIWebView to my current UIScrollView. The UIWebView is supposed to access a local html file 'index.html'. I got an error at runtime:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSURL initFileURLWithPath:]: nil string parameter'
This is my code:
UIWebView *webView = [[UIWebView alloc] initWithFrame:scrollViewFrame];
NSString *indexPath = [NSBundle pathForResource:#"index" ofType:#"html" inDirectory:#"Addition"];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:indexPath]]];
I do have 'Addition' folder (not group; imported from 'Add' in my project).
Thanx in advance...
Did you check indexPath with a breakpoint or NSLog? It looks like your file is not there (perhaps a typo?)-
EDIT
OK, I just tested this successfully. Try this:
Delete all the web files from your project.
Copy the web directory into the right place in your project folder.
Drag from Finder to Xcode, do not copy, Create folder references
One more caveat: Maybe there is a difference in capitalized resource names between the simulator and the device. Try using lowercase throughout.
Also, you should perhaps correct your command pulling the resource from the bundle. Instead of
[NSBundle pathForResource...
it should be
[[NSBundle mainBundle] pathForResource...

Nib files cannot be loaded by name from main bundle

It seems as though my nib files are included in my test target, they don't exist in the main bundle, so my app crashes on me when I am loding a nib by its name from the main bundle. I either need to find the correct bundle that includes my nib file, or I need to load a nib using a path.
Does anyone have a solution for either one? [NSBundle bundleForClass:[self class]] doesn't work. I think the nib and class files are not in the same bundle
It might help to enumerate the bundles
for (NSBundle *bundle in [NSBundle allBundles])
{
// can look for resources in bundle
locatedPath = [bundle pathForResource:resourcePath ofType:type];
// or maybe trying and load the nib from it?
UINib *nib = [UINib nibWithName:#"Blah" bundle:bundle];
// check for !nil ...
}