ios uiwebview stop request when shouldstartloadwithrequest - iphone

I am receiving shouldstartloadwithrequest event and I need to stop request for some conditions. How to stop uiwebview request when shouldstartloadwithrequest? Thank you

Check for the condition, then return NO in shouldStartLoadWithRequest. Make sure to set delegate appropriately and include the UIWebViewDelegate protocol in the interface of the delegate class.

You can invoke the stopLoading method for the UIWebView. Below code will check if the webview is loading and stop it.
if([webView isLoading]) {
[webView stopLoading];
}
EDIT: After re-reading your question, I understood you want to return NO in the request to stop it from beginning to load, so you would do something like:
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
//Check your condition:
if(myCondition == TRUE)
return NO;
return YES;
}

Related

ios - UIWebView DidFinishLoad not being called

I have a UIWebView that works ok. The delegate is working. When I open a URL, events shouldStartLoadWithRequest and webViewDidFinishLoad are fired.
But when for example I click on a Google search result, only shouldStartLoadWithRequest is fired, and webViewDidFinishLoad is never called.
This is causing problems because if I put a "loading..." indicator on shouldStartLoadWithRequest, in these cases the indicator still remains even after the page is correctly loaded.
Any idea why this is happening?
in .h file add <UIWebViewDelegate>
and in .m:
yourWebview=[[UIWebView alloc]init];
yourWebview.delegate=self;
When you click on search, the navigationType is UIWebViewNavigationTypeFormSubmitted. So in this case, the return type should be YES. If it is not, webViewDidFinishLoad method is not fired.
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSLog(#"shouldStartLoadWithRequest");
if ( navigationType == UIWebViewNavigationTypeLinkClicked ){
NSLog(#"UIWebViewNavigationTypeLinkClicked");
return YES;
}
if (navigationType ==UIWebViewNavigationTypeFormSubmitted ) {
NSLog(#"UIWebViewNavigationTypeFormSubmitted");
return YES;
}
if (navigationType ==UIWebViewNavigationTypeBackForward) {
NSLog(#"UIWebViewNavigationTypeBackForward");
return YES;
}
if (navigationType ==UIWebViewNavigationTypeFormResubmitted) {
NSLog(#"UIWebViewNavigationTypeFormResubmitted");
return YES;
}
if (navigationType ==UIWebViewNavigationTypeReload) {
NSLog(#"UIWebViewNavigationTypeReload");
return YES;
}
return YES;
}
This problem is usually found on ipad applications .... but you can solve your problem by using [self performselector:afterdelay] , you have to check in selector method whether webview has loaded or not using [m_pWebview loaded]; and according to that perform indicator stopping .... it's not the proper solution but it works .......
Not an expert on Google API, but as I suspected this might be your reason.

IOS - How to avoid keyboard from hiding when uiwebview reloads

I have a webview that loads a web chat client.
As every chat, the page has a textfield to input text.
The problem is that when the user opens the keyboard it is automatically hidden after a short time due to several ajax requests that are reloading the page. This becomes really annoying for the user as he or she can't input a complete sentence before the keyboard hides.
I don't know why, this only happens in iPhone 4S and iPhone 5. In iPhone 4, 3GS, and Simulator everything works ok.
I have tried to use shouldStartLoadWithRequest to catch the request and load it after the user hides the keyboard, but this ruins the chat session.
I tried to "hang" the request with a Thread sleep in the same method, but it happens in the Main Thread so it freezes the entire application.
Is there a way I can simply avoid the keyboard from hiding?
So after a long research I found a way to do it, its not the best but it helped me a lot.
First use DOM to check if the webView firstResonder
- (BOOL)isWebViewFirstResponder
{
NSString *str = [self.webView stringByEvaluatingJavaScriptFromString:#"document.activeElement.tagName"];
if([[str lowercaseString]isEqualToString:#"input"]) {
return YES;
}
return NO;
}
Then respond to UIWebViewDelegate method shouldStartLoadWithRequest, and return NO if UIWebView is first responder
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if([self isWebViewFirstResponder] &&
navigationType != UIWebViewNavigationTypeFormSubmitted) {
return NO;
} else {
return YES;
}
}
You can use Notification center inside your DidLoad method to listen when the keyboard will hide like this:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillHide)
name:UIKeyboardWillHideNotification
object:nil];
- (void) keyboardWillHide{
[webView becomeFirstResponder];
}
which will make the web view first responder and show the keyboard again. I haven't tried it myself so hopefully it will do the trick..
if your text field is on UIView
You can use web view Delegate method
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
[textField becomeFirstResponder];
}
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
[textField becomeFirstResponder];
}
otherwise if textField is on UIWebView then replace textField with webView as shown below.
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
[webView becomeFirstResponder];
}
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
[webView becomeFirstResponder];
}

Getting signals from uiwebview

Is there a possibility to get a signal from a uiwebview?
In my case, I display a webpage in the webview. When the user presses a button on the website, my App should (also) react to this.
Thanks for help
Yes you can do this. Implement
– webView:shouldStartLoadWithRequest:navigationType:
This delegate . This method gets called whenever your webview is about to make a request. So now when someone clicks a button on your webpage, you will get a call to this method. After you catch this call, you can choose to do whatever you want with it. Like redirect the link through your own servers, or log a request to your server about user activity etc.
Example - here you are trying to intercept any links clicked on your webpage & pass it through myMethodAction first.
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{
if(navigationType == UIWebViewNavigationTypeLinkClicked)
{
if(overrideLinksSwitch.on == TRUE)
{
[self myMethodAction];
[myWebView stopLoading];
return YES;
}
else
{
return YES;
}
}
return YES;
}
Hope this helps...
UIWebViewDelegate provide several methods. I had used one of them like this
-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
if ( inType == UIWebViewNavigationTypeLinkClicked ) {
// do stuff
return NO;
}
return YES;
}

UIWebView cannot click link

I am pretty sure that I understand how to catch a click on a UIWebView using the webView:shouldStartLoadWithRequest:navigationType: method, but my webView does not even allow me to click the link. I am using:
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(12, top, boundsSize.width - 40.0, 400.0f)];
webView.delegate = self;
webView.backgroundColor = [UIColor clearColor];
webView.opaque = NO;
webView.allowsInlineMediaPlayback = YES;
webView.dataDetectorTypes = UIDataDetectorTypeAll;
// add to subview
I am loading in an HTML string rather than loading a URL:
[webView loadHTMLString:bodyHTML baseURL:nil];
Delegate method:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSLog(#"Loaded");
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
NSURL *url = request.URL;
NSString *urlString = url.absoluteString;
NSLog(urlString);
}
return YES;
}//end
Everytime my webview loads I DO get the "Loaded" in my logs, so I know that the delegate method is getting called. But I can never click on a link within my UIWebView and have anything happen. It does not even look like it is allowing a pressed state on the link. The link is highlighted like a link, just won't allow clicking.
Ideas?
The reason for the UIWebView not responding to touch events is most probably this line:
webView.opaque = NO;
Try setting opaque to YES.
My understanding is that for a view to respond to touch events, that view has to be returned by the call to hitTest:withEvent: during the view hierarchy traversal performed by the event delivery mechanism.
From the hitTest:withEvent: documentation:
This method ignores view objects that are hidden, that have disabled user interaction, or have an alpha level less than 0.01.
Update
Based on new info: if the UIWebView is embedded in a UIScrollView, quote from the Apple docs:
Important: You should not embed UIWebView or UITableView objects in UIScrollView objects. If you do so, unexpected behavior can result because touch events for the two objects can be mixed up and wrongly handled.
Please try with following code
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if(navigationType==UIWebViewNavigationTypeLinkClicked)
{
[[UIApplication sharedApplication]openURL:request.URL];
return NO;
}
return YES;
}

Using a url scheme internally with a UIWebView to change a UILabel

I've got a UIWebView which shows up a couple of screens into a UINavigationController:
First View > Second View > View with UIWebView + UILabel
Now, I display a certain page in that web view, which has a link back to my app....
myapp://foofoo
I know you can set up a custom URL with (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url and some info.plist poking, but how would change the UILabel, which is one the same screen as UIWebView, by simply clicking the myapp://foofoo link?
The best way to do this would be through a custom link, and then use the UIWebView delegate method -webView:shouldStartLoadWithRequest:navigationType: to trap requests. When you see a request come through with your link in it, you know your action has been triggered.
UIWebView Expose Objective C to JavaScript
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSLog(#"scheme = %#",[[request URL] scheme]);
if([[[request URL] scheme]isEqualToString:#"myscheme"])
{
[messageLabel setText:#"HELLO!"];
return NO;
}
else return YES;
}
Make sure your view controller conforms to the UIWebViewDelegate protocol for this to work.