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

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?

Related

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

Force youtube video from UiView to play

I'm creating an app which include youtube videos. For this app i need a way to force the youtube video to open. how can i force it to start?
i've looked at this:
webView.userInteractionEnabled = NO;
But cant seem to figure out how to use it.
This is the code:
stringUrl = [NSString stringWithFormat:#"http://www.youtube.com/watch?v=%#", youtubeId[0]];
headerTitle.text = [NSString stringWithFormat:#"%#", theTitle[0]];
NSURL *url = [NSURL URLWithString:stringUrl];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];
Here you will get working example project in which it is using HCYoutubeParser to parse the youtube url
https://github.com/hellozimi/HCYoutubeParser
form here you can down load working code and you can check it on simulator and iphone both
even it will explain how to use it.

UIWebView local Resource main bundle html file

I've tried almost every way of loading an html file into an UIWebView that has images and resouces in the main bundle. The web page always loads but I can never get the images to show up.
NSString *html = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"];
NSString *htmldata = [NSString stringWithContentsOfFile:html encoding:NSUTF8StringEncoding error:nil];
NSString *path = [[NSBundle mainBundle] bundlePath];
[webView loadHTMLString:htmldata baseURL:[NSURL URLWithString:path]];
the HTML code has css:
background: url('image.png')
I've also assured that the file exists in the bundle when the app is run using other methods, but still no luck. Anyone see what's wrong with my code???
Instead of loading the HTML directly with loadHTMLString, you can have the web view do the work of loading the HTML from the bundled file. That way it should know where the HTML came from to resolve relative links.
Ex:
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"]];
[webview loadRequest:[NSURLRequest requestWithURL:url]];
I haven't tested the above, you may need the inDirectory clause of the pathForResource method. Good luck!
This is an old post, but I think a way to make this work is to change the following line:
[webView loadHTMLString:htmldata baseURL:[NSURL URLWithString:path]];
to:
[webView loadHTMLString:htmldata baseURL:[NSURL fileURLWithPath:path]];
The method in the comment and jimbojw's answer works too, but in my case htmldata is not retrieved from a file, but instead is in memory.

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

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.

Display PDF in UIWebView using loadData

I am trying to display a PDF I have stored locally in a UIWebView. This is how I currently attempt to do this:
if (![[NSFileManager defaultManager] fileExistsAtPath:self.url]) {
LOG_ERROR(#"Couldn't load local file. File at path: %# doesn't exist", self.url);
return;
}
nsurl=[NSURL fileURLWithPath:self.url];
NSData *data = [NSData dataWithContentsOfFile:self.url];
LOG_DEBUG(#"data length:%d",[data length]);
[self.webView loadData:data MIMEType:#"application/pdf" textEncodingName:#"utf-8" baseURL:nil];
I have also tried passing nil for textEncoding, as well as using UIWebView's loadRequest. The result is a UIWebView that displays a blank page. No errors occur in the UIWebView delegate method. The strange thing is that data has the correct length, in bytes, for the PDF I am trying to display, which means the file is being found and loaded correctly.
Does anyone have an idea as to what might be going wrong here or how I can better debug this problem?
Below you can find two different approaches to open a .pdf file on your UIWebView; one from a url and one as NSData:
if (self.pdfDataToLoad) //Loading the pdf as NSData
{
[self.webView loadData:self.pdfData
MIMEType:#"application/pdf"
textEncodingName:#"UTF-8"
baseURL:nil];
}
else //Open the pdf from a URL
{
NSURL *targetURL = [NSURL URLWithString:#"https://bitcoin.org/bitcoin.pdf"];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[self.webView loadRequest:request];
}
It turns out the problem was something to do with the file format of the PDFs. I edited them using Illustrator and re-saved them. I guess Mobile Safari doesn't like the way Illustrator formatted the files because each was blank when viewed using the simulator's browser (although I could open the PDFs in regular Safari just fine).
The solution was to open the PDFs using Preview and re-save them. After running each PDF through the Preview save routine I was able to get each PDF to display in a UIWebView without changing any of my code.
You don't need NSData to load local file, loadRequest should work directly with NSURL
[self.webView loadRequest:[NSURLRequest requestWithURL:nsurl]];
I can only suggest to make NSURL like that
nsurl = [NSURL URLWithString:[self.url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSData is used to load the pdf files when they are stored locally in any of the directories.
To load the pdf which was in your application, use the following code .
NSString *path = [[NSBundle mainBundle] pathForResource:#"FileNameHere"
ofType:#"pdf"];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];