Html files not working properly as same as browser in UIWebView - html

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.

Related

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

ios-css and js not working linked in html file when opening in UIWebview from Documents directory

I have following structure of my www folder in Documents directory in ios
Documents
--www
--index.html
--js
--script.js
--css
--style.css
My index.html file references script and style files as below:
<link rel="stylesheet" href="css/style.css" />
<script type="text/javascript" src="js/script.js"></script>
Please note, the files and directory structure are created after app launch, from a remote-downloaded and extracted zip file. So this is not an issue caused by Xcode folder groups being confused with the app bundle's Documents subfolders.
It works fine when run in a browser but when loading index.html in UIWebview programmatically, script and style are not working. Meanwhile the index file itself loads perfectly.
When I give full path of script and css in index.html file it working fine for me.
Why relative path not working?
Thanks in advance.
Apparently the document base is not the same as the app's Documents directory when loading the HTML file programatically.
Check out the BASE element in HTML, it goes within the <head> element:
http://www.w3.org/wiki/HTML/Elements/base
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>This is an example for the <base> element</title>
<base href="http://www.example.com/news/index.html">
</head>
<body>
<p>Visit the archives.</p>
</body>
</html>
To fix that, you'll have supply a Document base url manually. You haven't posted the code how you're programmatically loading the index.html file, so I will assume you're using the following UIWebView method:
- (void)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL
So now try out this code, see if it solves your problem:
// Get the app Documents path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
// Add www/index.html to the basepath
NSString *fullFilepath = [basePath stringByAppendingPathComponent:#"www/index.html"];// This must be filled in with your code
// Load the file, assuming it exists
NSError *err;
NSString *html = [NSString stringWithContentsOfFile:fullFilepath encoding:NSUTF8StringEncoding error:&err];
// Load the html string into the web view with the base url
[self.webview loadHTMLString:html baseURL:[NSURL URLWithString:fullFilepath]];
If that doesn't work, try updating the code of the HTML file that you get from the .zip file. Something like:
// Get the app Documents path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
// Add www/index.html to the basepath
NSError *err;
NSString *fullFilepath = [basePath stringByAppendingPathComponent:#"www/index.html"];
// Load the file, assuming it exists
NSString *html = [NSString stringWithContentsOfFile:fullFilepath encoding:NSUTF8StringEncoding error:&err];
// Check to ensure base element doesn't already exist
if ([html rangeOfString:#"<base"].location == NSNotFound) {
// HTML doesn't contain a base url tag
// Replace head with head and base tag
NSString *headreplacement = [NSString stringWithFormat:#"<head><base href=\"%#\">", fullFilepath];
html = [html stringByReplacingOccurrencesOfString:#"<head>" withString:headreplacement];
[html writeToFile:fullFilepath atomically:YES encoding:NSUTF8StringEncoding error:&err];
}
// Now the file has a base url tag
May be your JS And CSS File are not compiled by xcode.
Open your project with xcode and GO to 'Targets' and check in the 'Compile Sources' section . If your js file is present in that folder, move it to the 'Copy Bundle Resource' and delete is from "Compile Sources" folders.
After that delete app from your device, then Go to the Build menu in xcode and clean all Targets and recompile.
Check to see if your HTML file including with your js file.
May be its your trouble for loading js.
try this ...
i hope this will solve you problem
I also faced the same problem , I solved it by doing the simple thing when adding the files to XCode I did a small change that is shown in the figure below: you have to add files by choosing create folder references for any folders option
In the figure below one is for adding HTML , click copy also if you are adding file outside from project . if files are already present then no need to copy it again .

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

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.

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.