UIWebView load html file from bundle, and provide file with information? - html

I'm interested in replicating Mobile Safari's "Cannot Open Page" StandardError page.
In order to do that, I've been researching how mobile safari handle's an error when loading a page and opens the html file located in the application bundle.
In that bundle, the file: StandardError.html is loaded and produces an error while showing the mobile safari icon. The one thing that I'm having trouble with is understanding how the error is passed from the webview to the html file.
In the body of the html file, there is a variable with %#. I'm assuming that the %# indicates where Apple provides the error that caused the webview to fail loading. What I want to know is how to provide the title and error to an html file, similar to the StandardError.html file found in Mobile Safari's application bundle.
Any help would be greatly appreciated!

A UIWebView can load HTML from a string so you can do this:
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:#"StandardError" ofType:#"html"];
NSString* errorHTML = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
NSString *htmlString = [NSString stringWithFormat:errorHTML, weberror.localizedDescription];
[self.webView loadHTMLString:htmlString baseURL:nil];
Or something like that.

Related

Html files not working properly as same as browser in UIWebView

I have been working on one app in iOS platform where it need of display HTML files in WebView. Where my UIWebview is not working same as like browsers result.
i.e., Created folder reference in my xcode project and got the bundle path of the HTML file and loaded in UIWebView with NSURLRequest.
Is there any other way to run the HTML file in UIWebView like iphonehttpserver, GCDWebServer(But I am unable to get the demo code to start my server to achieve my requirement)
This is the code where I used to get HTML file path from resource bundle and loading in UIWebView (Folder name "assets" in which I have "web-app.html" file):
Objective-c:
- (void)viewDidLoad
{
[super viewDidLoad];
[self loadWebview];
}
- (void)loadWebview
{
NSString *htmlPath = [[NSBundle mainBundle] pathForResource:#"web-app" ofType:#"html" inDirectory:#"assets"];
NSURL *url = [NSURL fileURLWithPath:htmlPath];
[webVW loadRequest:[NSURLRequest requestWithURL:url]];
}
We found the issue, there was one servers.json file missing at our end. Because of that HTML unable to get the response to display the appropriate result.

WebView not loading an HTML file properly

I have a UIWebView in which I want to load an HTML file from an URL. This is the code I use to load the file with WebView :
NSURLRequest* blogRequest = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:self.url]
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:5.0];
[self.webView setDelegate:self];
[self.webView loadRequest:blogRequest];
The file I want to load (let's say http://example.com/foo.html) is nothing special, just plain text with basic HTML tag in it. The WebView is working properly for normal website, like google.com, but not my custom HTML file. All I get is an empty document.
webViewDidFinishLoad is never reach.
If I load my HTML file locally from my project, there is no problem. So, why isn't it working when downloading from internet? I have absolutely no clue.
Do you have a super slow network and your 5 second timeout is expiring?
Or, are you implementing a delegate method that is blocking the request?
Try this, it works for me:
NSString *URLString = #"http://example.com/foo.html";
NSURL *URL = [NSURL URLWithString:URLString];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
[self.webView loadRequest:request];

Loading external image in local HTML inside UIWebView

I have an NSString containing HTML string with external image URL. For example,
NSString *HTMLString = #"<p><img src="http://www.stackoverflow.com/image.jpg" /></p>";
When I use UIWebView to load HTML and present it in my app, the app freeze until the UIWebView done loading the web view.
Is that an issue about the download of image is not asynchronous? I try to find related topic but I can't get the answer.
How to make my image to be downloaded asynchronously?
Background: The HTML string is already loaded asynchronously using AFNetworking. All the text can be shown without loading the image. However, the process of downloading image freezes my app.
I don't really understand what you are trying to do, but if you want to show an image from a URL you can do that easily with a UIImageView like this:
imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:
[NSURL URLWithString:#"http://www.stackoverflow.com/image.jpg"]]];
Or if you want a UIWebView you could do a separate HTML file and load it like this:
NSString *path = [[NSBundle mainBundle] pathForResource:#"myFile" ofType:#"html"];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
[webView setScalesPageToFit:YES];
Or you can test to add tags like html, body etc.
I'm open for more info about your problem.

iOS7: Display an Image in an HTML File loaded into a UIWebView

I am programming in iOS7. In my app I am using a UIWebView to load static HTML files that are packaged with the app. I want to show images, also packaged with the app, in my HTML files, but can not find the magic combination to get the images to show when the HTML file is loaded.
I saw some information on base64, which I did not understand, but I would like to find a simple solution because there will be others beside myself writing most of the HTML files.
Below is the basic format of the HTML code I am using. I have tried all manner of “/“ modifiers to no avail, including the full path as listed in the File Inspector.
<img src=“data:/Supporting Files/Info/InfoRecycle.png">
Does anyone know how I get the image to show within the HTML file?
Try putting the image in the same folder as your html file. Then reference it like this:
<img src="InfoRecycle.png">
See if that works.
I finally got it to work! I found the key to the answer in this post. I guess I did not find it earlier because I was focused on iOS7:
Using HTML and Local Images Within UIWebView
I had to make some modifications in my Xcode, so it now looks like this:
//..Loads a Local HTML File
strHTML = #"Info";
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:strHTML ofType:#"html"];
NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
[_infoWebView loadHTMLString:htmlString baseURL:baseURL];
My reference to the image in the HTML code is now as suggested above by Stepan Grigoryan.
<img src="InfoRecycle.png">

Adding an image in an html file with iOS

In my app I'm presenting a report of the app data. The report is a "self-generated" html file presented on an UIWebView.
I need to include in the report (in the html file) an image that is stored in the device. At this moment I'm able to get the path of the image. It's something like "/var/mobile/Applications/A10781A1-DE2B-4651-ADFB-7A6AD9B3645A/Documents/EE20AF92-215E-4DF5-8E33-0713557A34C9"
How can I include the image in the html file?
You can include the image using:
<img src="img.png">
and
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
[webView loadHTMLString:htmlString baseURL:baseURL];
You can copy your image img.png to documents directory for example and set the baseURL to be the Documents directory.
Update:
I found the source where I found out how to do it some time back. Maybe you can dig from there some more useful info:
http://iphoneincubator.com/blog/windows-views/uiwebview-revisited
Its not clear if your building the html on the fly from your question. But you can do something like this.
NSMutableString *htmlPage = [[[NSMutableString alloc] initWithCapacity:1000] autorelease];
... // Build the page string
[htmlPage appendStringWithFormat:#"<img src=\"%#\" alt=\"image\" height=\"100\" width=\"100\"/>",[[NSBundle mainBundle] pathForResource:#"myImage" ofType:#"png"];
Or some variant of this.