How to reload already loaded uiwebview with new HTML string? - iphone

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()
})

Related

Why my page downloaded to folder Documents of iPhone simulator is not loadable?

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

How to clean the content of UIWebView without loading an empty page?

I need to clean the content of UIWebView(in order to reuse it), but I have some authentication handling code in its delegate methods, so I do not want to load an empty page like about:blank to clean it, as it will trigger my authentication handling code. So is there a way for doing this?
(also, by reusing it, I need to put a spinner on top of the web view, and when it loads another page, I don't want the user see the previous loaded page content, so that's why I need to clean it)
Thanks!
You can just use this line of code :
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"about:blank"]]];
Here you will find whole demo
this did the trick for me:
[webView stringByEvaluatingJavaScriptFromString:#"document.open();document.close()"];
Try this out. This worked for me
[self.webView stringByEvaluatingJavaScriptFromString:#"document.body.innerHTML = \"\";"];
If your only problem is that the spinner disappears when you load a blank page, you can use this code in webViewDidFinishLoad:
– (void)webViewDidFinishLoad:(UIWebView *)currentWebView {
if (![currentWebView.request.URL.description isEqualToString: #"about:blank"]) {
self.mySpinner.hidden = YES;
}
}
...then the spinner will go on until your webview is actually done loading.

UIWebView: Images are not updating

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

How can I only reload my localHTML anytime it is visited with UIWebView?

I'm making an iPhone app using xcode, but whenever the homepage (localHTML) is visited using the backbutton, the list item stays highlighted. My solution to this is to refresh the page whenever the localHTML is visited. Can anyone suggest an easy way of detecting when the url matches localHTML (should I use isFileURL?) Thanks for your help. Also, the list was made with javascript or else I would use deselectRowAtIndexPath.
This is what I came up with, but it doesn't work
-(IBAction)backbutton{
[webView goBack];
NSString *currentURL = webView.request.URL.absoluteString;
if (currentURL = "whatever the file path is") {
[webView reload];
}
is this a valid way to accomplish this? is there a way to get my localhtml's file path by using nslog?
Have you tried having a delay after going back, then refreshing?
eg. after
[webView goBack];
have a delay now using nstimer
then
[webView reload];

How do i insert UIImage on UIWebView

I'm trying to display an Animated-GIF image in a UIImageView. Because this way only the first frame is shown i wanted to try a different approach.
UIWebView.
How can i get an Animated-GIF that is in memory (NSData) to display on a UIWebView with all frames..?
Thanks Thomas for you response, i don't know if it works because i managed to do it myself now..
Been decoding the GIFs for more then a week now and still with no succes so i went from UIImageView to UIWebView and the following code made it work..
[self.webView loadData:gifData MIMEType:#"image/gif" textEncodingName:nil baseURL:nil];
I've not tried it, but something like this :
NSString *html = [NSString stringWithFormat:#"<img src='data:image/gif;base64,%#' />", [myData base64Value];
[myWebView loadHTMLString:html baseURL:nil]
You'll need a NSData category that implements converting to base64, that shouldn't be difficult to find (I've bookmarked http://cocoawithlove.com/2009/06/base64-encoding-options-on-mac-and.html)
edit: another way is to decode the gif, so you have an NSArray of the frames : http://pliep.nl/blog/2009/04/iphone_developer_decoding_an_animated_gif_image_in_objc
http://blog.stijnspijker.nl/2009/07/animated-and-transparent-gifs-for-iphone-made-easy/