OS X Web View App - Won't Load Local .html Web Page - objective-c

I have the following code in an AppDelegate.m file that opens Google when I open my app. I am trying to change it to open a .HTML web page I copied into my Xcode project but it is not working. I am using Xcode 4.5.2.
The code that works to open Google is:
#implementation MDAppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.google.com"]];
[self.webView.mainFrame loadRequest:request];
}
I tried changing it to (so that it would load my test.html file I put in the project folder but the app just comes up blank now). What am I missing?
#implementation MDAppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:#"test.html"]];
[self.webView.mainFrame loadRequest:request];
}

As long as your test.html is copied at the root of your project (make sure it's also copied to your final app bundle, huh? - check Build Phases) you can load it up your Web View using this code :
NSString *path = [[NSBundle mainBundle] pathForResource:#"test" ofType:#"html"];
NSURL* fileURL = [NSURL fileURLWithPath:path];
NSURLRequest* request = [NSURLRequest requestWithURL:fileURL];
[self.webView.mainFrame loadRequest:request];

[NSURL URLWithString:#"test.html"] is not creating a valid URL to your file. Try that: (it is from an iOS project, but should apply to OS X apps as well). I'm using loadHTMLString:baseURL: instead of loadRequest:
NSString* fileName = [[NSBundle mainBundle] pathForResource:#"test" ofType:#"html"];
NSString* htmlString = [NSString stringWithContentsOfFile:fileName encoding:NSUTF8StringEncoding error:NULL];
NSURL *baseURL = [[NSBundle mainBundle] bundleURL];
[self.webView loadHTMLString:htmlString baseURL:baseURL];

[NSURL URLWithString:#"test.html"]
There's your problem. Assuming that your test.html file is in the resource folder of your application bundle, do this instead:
[[NSBundle mainBundle] URLForResource:#"test" withExtension:#"html"];
NSBundle has a few other methods for easily creating URLs in case the one above isn't appropriate (like if the file is in a subdirectory). Take a look at the NSBundle reference page for more info.

Related

How to run a audio file again and again in ios sdk

SystemSoundID soundID6; // declaration
NSURL *gamePlayMusic = [NSURL fileURLWithPath:[[NSBundle
mainBundle]pathForResource:#"song1" ofType:#"wav"]];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)Music,&soundID6);
AudioServicesPlaySystemSound(soundID6); //running of music
may I know what to code to run this music again after it finish/complete.
1. Add AVKit.framework.
2.create a global webview object like UIWebView* audioPlayerView;
3.create a button action for playing audio file
4.your mp3 file add in to your bundel.
-(IBAction)playAudioFile:(UIButton*)sender
{
NSString *path = [[NSBundle mainBundle] pathForResource:#"your file name" ofType:#"mp3"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[audioPlayerView loadRequest:request];
[audioPlayerView setScalesPageToFit:YES];
[self.view addSubview:audioPlayerView];
}

Images in my html file in UIWebView not showing

Here is my scenario:
I have a folder called "HTML" in my bundle. This folder has an HTML file and 5 images. The HTML file references all the images at some point using a simple img tag.
<img src="tut_navigation.png" />
Now I have a UIWebview which I used to load the HTML file. All of the contents of the HTML file are rendered correctly except for my images. My images are not showing up for some strange reason. Then I did some research and found related posts saying to load URL like so:
NSString *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"tutorial" ofType:#"html" inDirectory:#"HTML"]];
NSString* htmlString = [NSString stringWithContentsOfFile:url encoding:NSUTF8StringEncoding error:nil];
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
[self.webView loadHTMLString:htmlString baseURL:baseURL];
Once I made this change and ran it again, this time only the first image is loading up, the others ones are not. I'm lost as to what the issue is. When I view this HTML file in a web browser, everything is working fine. Any ideas?
The baseURL needs to be the a path to the HTML folder inside the app bundle, not (as you have it) a path to the app bundle itself.
Change this:
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
[self.webView loadHTMLString:htmlString baseURL:baseURL];
To this:
[self.webView loadHTMLString:htmlString baseURL:url];
You had the right URL for the base URL (url) and then you just threw it away and substituted the URL of the bundle itself, which is wrong.
Even better, forget the base URL and just use a URL instead of a path string for the file itself:
NSString *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"tutorial" ofType:#"html" inDirectory:#"HTML"]];
NSURLRequest* req = [[NSURLRequest alloc] initWithURL:url];
[self.webview loadRequest: req];

Imbedded html file - what is directory?

This is my code:
-(void)viewDidLoad
{
[super viewDidLoad];
NSString *indexPath = [NSBundle pathForResource:#"iHelp" ofType:#"html" inDirectory:nil];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:indexPath]]];
}
The app is crashing because 'nil' for directory name is invalid (DUH!). So for an imbedded html file, what should go there?
UPDATE: found that "webView" has no outlet, although one is defined in the .h file... will close this because I believe there is an answer here. Will open a new question re: the outlets... thanks everybody!
If the file iHelp is in the main bundle, you can retrieve its path like this:
NSString *indexPath = [[NSBundle mainBundle] pathForResource:#"iHelp" ofType:#"html"];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:indexPath]]];
You can also do that like the following:
NSURL *indexURL = [[NSBundle mainBundle] URLForResource:#"iHelp" withExtension:#"html"];
[webView loadRequest:[NSURLRequest requestWithURL:indexURL]];
Drop the inDirectory: parameter as it will default to your current bundle. Note: it's an instance method.
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- (void)viewDidLoad
{
[super viewDidLoad];
NSBundle *bundle = [NSBundle mainBundle];
NSString *indexPath = [bundle pathForResource:#"iHelp" ofType:#"html"];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:indexPath]]];
If it isn't inside a folder reference, just omit that argument:
[[NSBundle mainBundle] pathForResource:#"iHelp" ofType:#"html"];
You can even go straight to URL:
[[NSBundle mainBundle] URLForResource:#"iHelp" withExtension:#"html"];

How can I load an HTML file?

How do I load an HTML file into my app (xcode)? I'm using the following code:
- (void)viewDidLoad
{
[super viewDidLoad];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"]isDirectory:NO]]];
}
When I start the app, I only see a white page. What's wrong?
.h file:
#import <UIKit/UIKit.h>
#interface poules : UIViewController {
IBOutlet UIWebView *webView;
}
#property (nonatomic,retain) UIWebView *webView;
#end
Here is a working example from one of my projects. My index.html file is in a folder called Documentation/html in the resources directory. Its important to note that these are "folder references", not groups (hence the blue icon):
then to load it in a webView:
NSString *resourceDir = [[NSBundle mainBundle] resourcePath];
NSArray *pathComponents = [NSArray arrayWithObjects:resourceDir, #"Documentation", #"html", #"index.html", nil];
NSURL *indexUrl = [NSURL fileURLWithPathComponents:pathComponents];
NSURLRequest *req = [NSURLRequest requestWithURL:indexUrl];
[webView loadRequest:req];
try loadHTMLString:baseURL: method
NSString *html=[NSString stringWithContentsOfFile:your_html_path encoding:NSUTF8StringEncoding error:nil];
[webview loadHTMLString:html baseURL:baseURL];
First thing you can check whether your webView is connected or not.
If it is then, you can break down your code to check what is wrong with request that you are trying to load.
1.. Create a NSString for the file path like this, and check if the path is returned or not.
NSString *urlString = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"];
2.. Create a NSURL from the above string like this, and check if the url is correct or nil.
NSURL *url = [NSURL URLFromString:urlString];
3.. And then create the request.
NSURLRequest *request = [NSURLRequest requestFromURL:url];

pages.zip file will fail to displays view using UIWebview and NSURL

I have an application that displays search tips. This will work if I store an .html file in the main bundle and display it using the Apple example code:
-(void)loadDocument:(NSString*)documentName inView:(UIWebView*)webView
{
NSString *path = [[NSBundle mainBundle] pathForResource:documentName ofType:nil];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
}
//load document --
[self loadDocument:#"searchTipsLegislators.html" inView:searchTips];
If however I change the document to one created using a compressed Pages file the app return the error:
2010-12-15 08:53:04.537 cv112[42067:7003] Cannot load iWorkImport
2010-12-15 08:53:04.538 cv112[42067:7003] Failed to generate preview
Has anyone gotten this to work?
Two things to try:
Don't compress the pages document. This is no longer necessary as of iOS 3.
Don't expect this to work on Simulator. Does it work on your device?