UIWebView get user touch on tapping link - ios

I want to perform some action when the user clicks any link on UIWebView. I am doing this for making this work :
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSLog(#"request - %#", request.URL.absoluteString);
[webView.window makeKeyAndVisible];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
if(isFirstTimeLoaded)
{
[self shoToolbar];
[self updateButtons];
}
return YES;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
isFirstTimeLoaded = YES;
}
This usually works fine. But it doesn't when UIWebView's first load involves multiple redirection while loading the page. What I want is to fire this condition only when the user intentionally clicks on the link. Not on the url redirection of UIWebView.

You have to check navigationType
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if (navigationType==UIWebViewNavigationTypeLinkClicked) {
// It was a link
} else {
// It wasn't a link
}
return YES;
}

Related

SVModalWebViewController - Pass URL from UIWebView

I have a main UIWebView and I am trying to open any URL using SVWebViewController. My code can be seen bellow.
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if (navigationType == UIWebViewNavigationTypeLinkClicked ) {
SVModalWebViewController *webViewController = [[SVModalWebViewController alloc] initWithAddress:#"http://google.com"];
[self presentViewController:webViewController animated:YES completion:NULL];
return NO;
}
return YES;
}
It seems like SVWebViewController has an initWithAddress parameter. I want to know if I can pass the requested URL in that parameter.
Thank you in advance.
Found a way out of this. It seems like I could work with
initWithURLRequest:(NSURLRequest *)request
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if (navigationType == UIWebViewNavigationTypeLinkClicked ) {
SVModalWebViewController *webViewController = [[SVModalWebViewController alloc] initWithURLRequest:(NSURLRequest *)request];
[self presentViewController:webViewController animated:YES completion:NULL];
return NO;
}
return YES;
}

Call Objective C Method from URL webView

I have a webView which has URLs link to KEYWORD. Upon clicking the URL, I would like to call the following method:
[self.db fetchDefinitionsWithKeyword:#"KEYWORD" callback:^(NSDictionary *response) {
NSString *HTML = [self.HTMLRenderer renderHTML:response];
[self.webView loadHTMLString:HTML baseURL:nil];
In Android, we can use shouldOverrideUrlLoading. How should I do that in Objective C?
Try this code may be its helpfull for you.
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if (navigationType == UIWebViewNavigationTypeLinkClicked )
{
// you can call any thing on click.
[[UIApplication sharedApplication] openURL:[request URL]];
return NO;
}
return YES;
}
you can use this method
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
if you want do at your click, you can judge the navigationType is UIWebViewNavigationTypeLinkClicked. otherwise you can ignore navigationType.
all webview request go through this method . good luck.

How to Load Only .html page in WebView and All www Page load in Safari in iOS?

i am newbie in iOS Development I load my HTML Data Into WebView But Some time it Contain only href link as .html link and some time website link like as www.google.co.in so i want to load only html data in to Webview and any website are load in to Safari for that i write a code like as
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSString *link = [[request URL] relativeString];
if ([link isEqualToString:#"module1learningobjectives.html"])
{
return NO;
}
else
{
[[UIApplication sharedApplication] openURL:[request URL]];
return YES;
}
return YES;
}
then it load .html file in web view but site are open in safari and Webview both i want only site was open in safari please give me solution for that.
I'd do something like this:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSLog("URL is %#, and has an extension of %#", request.URL, [request.URL pathExtension]);
if ([[request.URL pathExtension] isEqualToString:#".html"])
return YES;
return NO;
}
Is this what you are asking?
If you want it to use Safari for non files and your own WebView for files, then try this:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if ( ! ([request.URL isFileURL]) ) {
[[UIApplication sharedApplication] openURL:[request URL]];
return NO;
}
return YES;
}
Just modify your code to below code.
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if (navigationType == UIWebViewNavigationTypeLinkClicked)
{
NSString *strLink=request.URL.absoluteString;
if([strLink rangeOfString:#".html"].location!=NSNotFound)
{
[[UIApplication sharedApplication]openURL:[request URL]];
return NO;
}
else
{
return TRUE;
}
return NO;
}
return YES;
}
Above code check if the link which is going to open has .html extension or not? and it works accordingly.
One more thing in your question you haven't mentioned that the href are link of local pages or external links.
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *url = request.URL;
NSString * temp = [NSString stringWithFormat:#"%#",url];
if ([temp rangeOfString:#"www"].location != NSNotFound)
{
// show alert view for go to safari
// i.e
[[UIApplication sharedApplication] openURL:url];
}
else
{
// your regular html page pushed
}
}

remove unsafe: from NSURL and load rest of URL webview

I have a url like unsafe:customscheme://customResourceSpecifer. I want to remove the unsafe prefix from it and load the rest of url, currently I am doing like this, is there any other way?
-(BOOL) webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if ([request.URL isUnsafeURL])
{
[self.webView loadRequest:[request.URL URLbyRemovingUnsafePrefix]];
return NO;
}
return YES;
}

UIWebView override url

I want to create an iPhone WebView App. But there is a problem, if I click on some buttons, it opens the normal and not the mobile version.
I did create the same WebView app on Android and there I solve the problem with this code
webView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
if(!url.toLowerCase().contains("http://www.example.com/"))
{
webView.loadUrl(url + "?mt=1");
return true;
}
return false;
}
});
How can I do this on xcode?
Thank you very much
Best wishes
Dominik
You can do the same using the UIWebViewDelegate protocol.
I think you can achieve the required result by changing the NSURLRequest within the delegate function
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request.......
//delegate methods
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
//use NSURLRequest object request , to manage the request.
//use NSURLRequest object request , to manage the request.
NSURL *urL=request.URL;
NSString *urlStr=[urL absoluteString];
NSLog(#"URLL %#",urlStr);
if([urlStr isEqualToString:#"http://www.google.com/"]){
urL=[NSURL URLWithString:#"http://www.yahoo.com"];
request=[request initWithURL:urL];
[webView loadRequest:request];
}
return YES;
}
- (void)webViewDidStartLoad:(UIWebView *)webView
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
//start of request
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
//finished loading
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
//error
}
This will work for sure. I am using above code to load www.yahoo.com when www.google.com is requested.

Resources