I have a local HTML page which doesn't has any external link(css or images..)
I use method below to load it to a WebView
- (void)loadData:(NSData *)data MIMEType:(NSString *)MIMEType textEncodingName:(NSString *)textEncodingName baseURL:(NSURL *)baseURL;
it takes around 5-8 seconds to load this page, that's too slow. right?
but when I load that page in iPhone safari, it takes around 2 second.
I don't know why it is slow like that in my WebView.
I also did a Google search, but I couldn't find any answer.
To load local html file you should try this:
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"]isDirectory:NO]]];
If you have the HTML as text then use the loadHTML:
[webView loadHTMLString:#"<html><body>Some HTML</body></html>" baseURL:nil];
It loads about 2 seconds for me (simple HTML).
Maybe you have some very large HTML and you read it from file?
Related
I made an hybrid app between objective C and javascript. My app works so:
When I press on the first button it will load a UIWebView
In this UIWebView I can save the html to the Documents folder
When I save the website I create a JSON to store informations and I save this JSON to Documents folder
When I press the second button, it will load another UIWebView in which there are a mobile site that read informations from JSON and present this data in a html list
When I press on the name of one site it will load the site from Documents folder
The first 4 points works great, but when I press on the site title I've trouble, indeed, the UIWebView shows me a page in which there are written that the page it's not stored on this server. When I navigate with Finder to the Documents folder of my app and I double click on the html site it shows me it in Safari. Why when I load it with an UIWebView doesn't work? I will post here a screenshot to understand my flow.
CODE: I post here the code to load the html:
viewDidLoad method where I call the method to load my page
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.webView.delegate = self;
//[self loadWebsite:#"http://localhost/root/main/index.html"];
[self loadWebsite:#"http://192.168.2.1/root/main/index.html"];
}
loadWebsite method
-(void)loadWebsite:(NSString*)site
{
NSURL *url = [NSURL URLWithString:site];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webView setScalesPageToFit:YES];
[self.webView loadRequest:request];
}
This seems like a problem with how you are loading your html file into the webview. You should post the code where you are loading the uiwebview.
To load a webview with a local file use the following code :
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"htmlfilename" ofType:#"html" inDirectory:#"Documents"]];
[webview loadRequest:[NSURLRequest requestWithURL:url]];
You can refer to this post : Load resources from relative path using local html in uiwebview
I am using UIWebView to load HTML Page. From that HTML I have generated PDF file. My PDF file content changes as per the HTML data changes. when I change the Signature(Image) of the user in application respective PDF will not change to new signature. It shows the previous signature.
How can I reload that UIWebView?
[webView reload] should do the trick.
If you are using a new URL you can use [webView loadRequest:[NSURLRequest requestWithURL:theURL]];
If you are using same url with updated content use this.
UIWebView* webV=[[UIWebView alloc]init];
NSString* htmlString= #"Loading...";
[webV loadHTMLString:htmlString baseURL:nil];
[webV loadRequest:[NSURLRequest requestWithURL:mainURL]];
When I load an .html file into webview using following method and finish its load, an event -(void)webViewDidFinishLoad:(UIWebView *)webView { is fired.
[webView1 loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"/files/myFile" ofType:#"html"]isDirectory:NO]]];
However, if I load same file remotely, that event is not fired! why? How could I do it? Thank you.
[webView1 loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[urlBase stringByAppendingString:#".html"]]]];
The most likely reason is because of an error during the load.
Try and define the delegate method – webView:didFailLoadWithError: and see what happens (if it is called instead of didFinish)...
I'm wondering whether there's a control to display html embedded data in iphone. I need to display the information as it is on the web. The data is saved into it database together with its html tag.
So is there anyway for me to display the data accordingly..
You can use a UIWebView and load HTML using
- (void)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL
you can use a UIWebView and loadHTMLString method to load the data in HTML.
You can also change html page contents at run time using the javascript.
You can use webview for displaying displaying html embbed data.
Example is given here :
To load remote html file:
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.apple.com"]]];
To load local html file:
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"]isDirectory:NO]]];
To load html string:
[webView loadHTMLString:#"<img src=test.png>" baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];
Is it possible to preload content of a local file with embedded images for a UIWebView?
This is how I'm loading the data inside the UIWebView:
NSString *urlString = [[NSBundle mainBundle] pathForResource:#"info" ofType:#"html"];
NSString *htmlContent = [NSString stringWithContentsOfFile:urlString encoding:NSUTF8StringEncoding error:nil];
[myWebView loadHTMLString:htmlContent baseURL:nil];
But the UIWebView still needs some time to display the content. Is there any way to put this content into the browser cache on app start?
If you create the browser object on app start and load the HTML string immediately, it should work fine to display the view later.
Another option would be to allocate it when necessary, but not actually show the view until it's done loading (wait for - (void)webViewDidFinishLoad:(UIWebView *)webView to be called on the web view's delegate before showing it).
Also, would it make more sense to call
[myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:urlString]]];
than using the loadHTMLString: method? That way, you don't have to load the HTML string into memory yourself and the web view can load it directly from disk.