How to call a function after UIWebView shouldStartLoadingWithRequest method is finished executing in iOS?
I dont want to use performselector:afterdelay because that may not give accurate result while network is low.
Use webViewDidFinishLoad: this method is call after a web view finishes loading.
- (void)webViewDidFinishLoad:(UIWebView *)webView
Parameters
webView:
The web view has finished loading.
Availability
Available in iOS 2.0 and later.
use these methods may be it helps you,
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
return YES;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
//this method will call after successful loading
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
//this method will call after getting any error while loading the web view.
}
The UIWebViewDelegate Protocol provides a few methods which can be used in your situation.
The first method that is called after shouldStartLoadWithRequest is webViewDidStartLoad: which, like its name suggests, is called once the WebView has started loading a frame.
From your question, this seems to be the most adequate method.
Other options, which were provided in previous answers, are webViewDidFinishLoad: and webView:didFailLoadWithError: which are respectively called every time the UIWebView finished loading a frame and whenever it fails to load a frame.
Note that this means that all methods described above may get called more than once before the UIWebView has actually finished loading, since they are thrown whenever the UIWebView is about to start, has just started, has finished or has failed loading each frame on a page.
Refer to the UIWebViewDelegate Protocol documentation for more detailed information.
Related
I need to display webpage using URL. From that page I want to remove one link control from that html page. Is it possible? I am opening it with this kind of code.
NSString *urlAddress = #"http://myurl.com";
NSURL *url = [NSURL URLWithString:urlAddress];
If you want to disable ALL links, see the answer from user3182143...
If you only want to disable a specific link, you can still use those methods - you will just need to evaluate the request part of shouldStartLoadWithRequest:(NSURLRequest *)request and decide whether to return YES or NO
If you want a specific link to look different, you'll need to modify the html. Couple ways to do so, but we really need more information about exactly what you are trying to do.
Are you displaying ONE specific web page? And all you need to do is change ONE link in that ONE page, and you're done? Are you displaying a series of pages? Do you have any control of the pages? And so on.
I'd suggest you start searching for and reading about uiwebview insert html or uiwebview insert javascript
We can do this.
First we should know about the UIWebViewDelegate methods
First create BOOL
ViewController.h
#property (nonatomic) BOOL ret;
ViewController.m
#synthesize ret;
Set BOOL to NO in below delagte method
- (void)webViewDidFinishLoad:(UIWebView*)webView {
// Sent after a web view finishes loading a frame.
ret = NO;
}
Then If we return NO in below delegate method, this will prevent the user from viewing link
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
return ret:
}
Dave DeLong's Disable Link in UIWebView answer
Removing hyper links from a URL shown in UIWebView
I am currently embedding a UIWebView inside a UICollectionViewCell to be able to view HTML formatted text residing in my local data structure.
I know it has been stated many times one should not embed an UIWebView inside a scrollView, but i currently see no other way to achieve displaying mixed content overviews (That is the current requirement, and so far UICollectionView does work very well with large data sets).
The Problem is, that i am not able to call the UIWebViews Delegate method:
- (BOOL)webView:(UIWebView *)webView
shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType { ... }
I need the delegate method to be able to display external links or call local methods.
Is there a way to achieve this? Maybe by handing over the events programatically?
Thanks for the feedback!
I just found out what i did wrong:
I just needed to update the cells' contentView: [[cell contentView] setFrame:frameContainingWebViewSize]
That did the trick. The delegate method was not called because the contentView did not cover the webview (clipSubviews is set to NO) and thus the tap events where not fired.
First thanks in advance to have a look on my issue. My issue is my am developing webview based application. It's same like as browser but i have a issue related to reload web view. When i am getting a value form popover bookmarked and try to reload the current web view from this next bookmark url, my web view do noting. I am stuck in this issue. Although when i enter any url through it works perfectly. May i doing something wrong with web view ?
temp = appDeleg.strURLFromPopover;
NSURL *url = [NSURL URLWithString:temp];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[self.myWebView loadRequest:requestObj];
[self.myWebView setDelegate:self];
[self.view addSubview:myWebView];
Please have a look..
The last line looks like very suspicious. In my experience, [self.view addSubview:myWebView] should be placed in the "initialization block" to avoid some lazy init issues. Are you sure All objects are NOT nil? if so, you can try to set UIWebView's delegate to your class, and debug in each method to test if your object are properly initialized. I tried your code, work fine in my side. Below are delegate method you might consider about
– webView:shouldStartLoadWithRequest:navigationType:
– webViewDidStartLoad:
– webViewDidFinishLoad:
– webView:didFailLoadWithError:
I am wondering if there is something already like this out there. But I basically want something like UITextView to display text with embedded links. However, I want to be able to handle the URL clicks as a delegate.
I've read a few posts like the following:
How to intercept click on link in UITextView?
However, I really don't want to override the openURL method. My app works with lots of webServer data, and I don't want to keep creating exceptions for different hosts in the openURL method.
I guess my questions is, is there another way to intercept the click on UITextView?
My alternative is to write my own, with a UIScrollView, and use a TTTAttributedLabel (https://github.com/mattt/TTTAttributedLabel) inside it. But am looking for suggestions, or alternatives.
Thanks.
You may use UIWebView + Local HTML instead of UITextView.
And use the -(BOOL )webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType )navigationType delegate to handle the URL clicks,like:
-(BOOL )webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType )navigationType
{
NSURL * clickedURL=[request URL];
//Do something here.
return NO;
}
Is it necessary to do:
[webView stopLoading];
self.webView = nil;
Or, can I leave out the first line? I.e. does -[UIWebView dealloc] call -stopLoading for you?
-stopLoading might be neccasary because loading is done in a seperate thread where uiwebview is retained (as a callback of a web-request afaik). if you release a uiwebview (e.g. in a -viewDidUnload method) i'm pretty much sure that it will stop loading but when it stops loading, it will call its delegate.
This will fail if you don't set the yourWebView.delegate = nil because the delegate property is defined as assign-property and therefore does not retain your delegate object (most probably a uiviewcontroller). not setting the delegate to nil will result in EXEC_BAD_ACCESS (you can actually kill some apps where you can switch between subviews containing webviews very fast - they get released before they finish loading and try to call a zombie delegate). same applies to MKMapView!
The web view will stop loading you do not need to call stopLoading. It is not stopped in dealloc though. I believe it stops when it is removed from its superview.