Error implementing UIWebView to load local html - objective-c

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...

Related

NSString pathway always returns nil despite having the file contained in the folder

So i'm trying to create a video screen saver for macOS 10.12 by using NSString to obtain the pathway, and NSURL to store the pathway.
However every time I test the screen saver, it crashes with this error:
Application Specific Information:
com.apple.preference.desktopscreeneffect v.5.1 (Desktop & Screen Saver)
Crashing on exception: *** -[NSURL initFileURLWithPath:]: nil string parameter
I believe the problem lies here:
// Create the video path then use it. (Problem at the moment: the pathway always returns nil)
NSString *screenSaverPath = [[NSBundle mainBundle] pathForResource:#"apple" ofType:#"mp4"];
NSURL *screenSaver = [NSURL fileURLWithPath:screenSaverPath];
//Obtain the size of the screen.
NSSize size = [self bounds].size;
//Draw the video player.
AVPlayer *video = [AVPlayer playerWithURL:screenSaver];
But I am unsure as to how I can tackle this problem. Does anyone know any way as to how you can resolve this problem? Thank you!
Screen savers are loaded into an application. So mainBundle is the ScreenSaverEngine process that loaded your plugin, not your plugin. You should probably use bundleForClass to get the bundle your screensaver's main class is in.

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"];

Loading nib from my own bundle?

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.

Could not load NIB in bundle ImageViewCell

I'm getting this error while running simulator. The project was not created on this environment, not sure I'm missing a configuration setting.
Error
2012-08-08 19:30:56.411 ACME[4068:f803] mItemArray.count: 2 2012-08-08 19:30:56.413 ACME[4068:f803] * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle (loaded)' with name 'ImageViewCell''
if(cell == nil)
{
NSArray *array = [[NSBundle mainBundle] loadNibNamed:#"ImageViewCell" owner:self options:nil];
cell = [array objectAtIndex:0];
}
You have not set the "files owner" in the NIB file to the owner you are setting in code. The NIB loader will fail like this when it cant link the objects in the NIB to the right root owner.
The name after "loadNibNamed:" must be the name of the nib file (in project navigator on the left side of the screen), sometimes people write there 'cell identifier' that writes from the start in CellForRowAtIndexPath (static NSString ...).
May be your problem in this little fault?

Adding CoreData to existing Project

I am trying to add CoreData to an existing project and
there is strange error...
I get the error:
Terminating app due to uncaught exception
'NSInvalidArgumentException', reason:
'Cannot create an
NSPersistentStoreCoordinator with a
nil model'
in RootView there is a warning in:
- (NSFetchedResultsController *)fetchedResultsController;
in line:
_fetchedResultsController.delegate = self;
and it says:
warning: class 'RootViewController'
does not implement the
'NSFetchedResultsControllerDelegate'
protocol
any ideas?
argh!
sorry for even asking, I just missed that I changed the name of .xcdatamodel
I needed just to change the name in managedObjectModel and persistentStoreCoordinator
hope it'll help someone anyway.
I was getting the same exception.
My issue was that I had added the .xcdatamodeld file in the root directory of the project (with siblings Frameworks and Products) instead of in the folder named after the project (with siblings Supporting Files, AppDelegate.h, etc.)
I tracked it down to this line: NSURL *modelURL = [[NSBundle mainBundle] URLForResource:#"Foo" withExtension:#"momd"];
In this method: - (NSManagedObjectModel *)managedObjectModel