iOS webview event when remote html file finish loading - iphone

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)...

Related

UIWebView back button not getting enabled

In my application I have a UIWebView in which I need to add browser like functionality(user can goBack,Forward or reload the page). To achieve this I have used the code for this tutorial.
In my code I have only one change in viewDidLoad:
I am loading data from a local html file like this:
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:#"File name" ofType:#"html" inDirectory:NO];
NSData *htmlData = [NSData dataWithContentsOfFile:htmlFile];
[self.webView loadData:htmlData MIMEType:#"text/html" textEncodingName:#"UTF-8" baseURL:[NSURL URLWithString:#""]];
instead of:
NSURL* url = [NSURL URLWithString:#"http://iosdeveloperzone.com"];
NSURLRequest* request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];
After loading initial html file if I am making any click on that page than the back button should get enabled, instead of its getting enabled after second url click and so that I am not able to go back to the original home page.
Please help me with this.
Thanks.
I got an answer. It's because you load data into the webView without loadRequest. If you load data into webView as a string, then it won't have any url to go back or load the old page. So it won't store the data you given to the webView. For that, you need to provide the url as like this.
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"File name" ofType:#"html"] isDirectory:NO]]];

How to reload already loaded uiwebview with new HTML string?

I have implemented UIWebView in my project to show some HTML data. The problem is I have loaded the webview with some HTML string by using the method
[myWebView loadHTMLString:Data baseURL:nil];
Now I want to add action of button such that when user click on the button the new data is loaded in the webview. such that
-(void)clicked{
[myWebView loadHTMLString:newData baseURL:nil];
}
I tried by applying the same as above but no affect was there. I also tried [myWebView reload], but nothing happened.
Plz tell me the appropriate method.
TRy with, First call stopLoading then reload on your UIWebView instance.
[myWebView stopLoading ];
[myWebView reload];
loadHTMLString:baseURL: is the correct method.
But still some things might be wrong, such as the data you are trying to send to the webview. Did you try logging your newData object to see what is being send to the webView object?
Is your newData string loaded from a resource file? If so, you could try loading you html data from the resources with something like the code bellow
NSString *html = [[NSString alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"myFileName" ofType:#"html"]];
[myWebView loadHTMLString:html baseURL:nil];
Even after trying accepted answer I have not got my issue solved.
So, after spending some time I found below logic which works for me:
Objective-c
dispatch_async(dispatch_get_main_queue(), {
[myWebView reload];
})
Swift
dispatch_async(dispatch_get_main_queue(), {
myWebView.reload()
})

display html embedded data in iphone

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

how to load an html file locally into the uiwebview

i am having problem with how to get the uiwebview loaded with local html file .provide the cod e as well i am having the hmtl file as fp_dis-1.html.
thanks in advance
Try this,
UIWebView *page;
[page loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"your-local-file-url" ofType:#"html"]isDirectory:NO]]];

Is there any way to make UIWebView faster

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?