How to call a method when a URL is hit inside UIWebView? - ios

I am loading a URL inside UIWebView. It opens a web-page which contains some URL links. Is it possible that I call a method when particular link is clicked.
I simply want to detect the URL clicks inside UIWebView in my iOS App.
Thanks.

Yes, there is method in UIWebViewDelegate protocol. Add your controller as delegate to UIWebView.
self.webView.delegate = self;
Then implement delegate's method:
- webView:shouldStartLoadWithRequest:navigationType:`
You should read it's documentation. As you can see, one parameter is navigationType, you interested only in UIWebViewNavigationTypeLinkClicked.

Like Timur says add into your viewController the UIWebViewDelegate
Then you can use the method like this:
-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
if ( [[[inRequest URL] scheme] isEqualToString:#"js-call"] ) {
// Do something interesting...
return NO;
}
return YES;
}
Then i've used a js call, but you can use also links:
window.location.href = "js-call:myEvent";

Related

YTPlayerView once loaded opens the video in youtube app

So the title pretty much sums up the situation I am loading my YTPlayerView like this:
[self.videoView loadWithVideoId:#"youtube id"];
And like it was mentioned in the documentation it works perfectly however the second time I tap on the view the video opens on the youtube app sometimes and not on fullscreen.
Any help here ?
After a lot of research I didn't find an answer to this question so I decided to dig on the YTPlayerView.m file and see how the touch event is handled and I found that there is two ways to do this:
1- the YTPlayerView contains a webView and an override of:
- (BOOL)webView:(UIWebView *)webView
shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType
This is the full code which checks the url scheme to trigger the appropriate action
- (BOOL)webView:(UIWebView *)webView
shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType {
if ([request.URL.host isEqual: self.originURL.host]) {
return YES;
} else if ([request.URL.scheme isEqual:#"ytplayer"]) {
[self notifyDelegateOfYouTubeCallbackUrl:request.URL];
return NO;
} else if ([request.URL.scheme isEqual: #"http"] || [request.URL.scheme isEqual:#"https"]) {
return [self handleHttpNavigationToUrl:request.URL];
}
return YES;
}
which was in my case this function handleHttpNavigationToUrl that returns a bool which indicates if the video should open on the app or the youtube application.
I have commented these lines that opens the video on the app and everything is ok now.
[[UIApplication sharedApplication] openURL:url];
return NO;
This is a quick fix and not good one i know but in my case i had no choice the 2nd solution is much better though.
2- The problem is in the youtube url in the first place as if you are using the embed url this problem will not occur,
so instead try to use other methods for loading the video by using the embed url, which is of the format:
http(s)://www.youtube.com/embed/[VIDEO ID]?[PARAMETERS]
so just try to use loadVideoByURL and use the embed url instead of loadWithVideoId.
Voila i hope this helps.

iOS : How to override the button action of UIWebView and call our own Objective C method

I am loading a url in UIWebView in iOS. the url contains a button. I want to call a objective C method, when i click on this button. Please help.
This is source code
NSString *strUrl = #"<html><body><button type=\"button\" id=\"success_id\" style=\"width:200px;margin:0px auto;text-align:center; background:#1B2F77;color:#fff;padding:10px;font-size:10px;border:0px;\" name=\"continue\" onclick=\"ok.performClick();\">Continue</button></body></html>";
[_tempWebView loadHTMLString:strUrl baseURL:nil];
I want to load this string to UIWebview and When I click on button , then I want to open my own viewcontroller.
You need to do three things:
Override shouldStartLoadWithRequest delegate method of UIWeBView.
shouldStartLoadWithRequest method returns a bool value so return false, then it will not load the link.
Inside this method write whatever functionality you want to perform.
If it is your page, communicate with app via JavaScript otherwise you can override shouldStartLoadWithRequest method and check if this specific request is called
some code:
- (BOOL)isItThatButtonEvent:(NSURLRequest *)req {
if([req.URL.absoluteString isEqualString:#"that.specific.url.or.some.other.way.to.check.that"]) {
return YES;
}
return NO;
}
#pragma mark UIWebViewDelegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if([self isItThatButtonEvent:request]) {
[self methodThatOpensYourViewController];
return NO;
}
return YES;
}

Google Login with GIDSignIn didn't have back button

I used the GPPSignIn to login google in the first time.
But it's rejected by App Store because it will open the Safari when login
So I replace the GPPSignIn with GIDSignIn.
And it works! It can login with WebView!
But the question is.... how should I back when I open the WebView!
Or say how should add the "Back Button" in this WebView
Thanks for your helping :)
You can Try html markup such as My Button into the content of your UIWebview using stringByEvaluatingJavaScriptFromString method.
To capture the click event and prevent it from reloading content of your webview use this delegate:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if(navigationType==UIWebViewNavigationTypeLinkClicked && [[request.URL absoluteString] isEqualToString: #"yourTag01"])
{
//your action code goes here
return NO;
}
return YES;
}
For Action of your button You can use this :
if ([yourWebView canGoBack])
{
[yourWebView goBack];
}
I recently had a similar problem. I solved it with import libraries SafariServices.framework and run app again.
http://i.stack.imgur.com/eonwU.png

Is it possible to intercept the click handler in links of WEBUI's, and add a custom click action?

Say there's a custom link embedded in a WebUIView, and I would want to redirect a user not to Safari but to a different screen in the app. Is it possible to change the click handler for that link?
You can assign a delegate to the webView and use the webView:shouldStartLoadWithRequest:navigationType: method to detect the links you want a return NO.
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if ([[request.URL absoluteString] isEqualToString:#"http://www.customurl.com"]) {
// Do something
return NO;
} else {
return YES;
}
}
You may also want to check the navigationType against UIWebViewNavigationTypeLinkClicked.

WebView:How to detect if I am on the original web page and execute a certain method

I have this method that goes back to previous webpage in a web view under certain circumstance and it works fine. However, I want to implement a function that checks if I am on the original web page and if so, when I push the back button it takes me to a navigation view controller. The original page is this code [webView loadHTMLString:self.item.description baseURL:[NSURL URLWithString:self.item.link]];. Now I need to figure out how to check if I am on that page and if so I need to execute this code: [self.navigationController popViewControllerAnimated:YES];. Any help will be appreciated.
- (void)back
{
if ([webView canGoBack]) {
[webView goBack];
} else {
[webView loadHTMLString:self.item.description baseURL:[NSURL URLWithString:self.item.link]];
}
}
You can use the delegate method shouldStartLoadWithRequest of UIWebViewDelegate to check what contains in the url. if you find it then just go NavigationController
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
if ([[request.URL absoluteString] rangeOfString:#"http://yourURL"].location!=NSNotFound) {
[self.navigationController popViewControllerAnimated:YES];.
}
}
You need to add
<UIWebViewDelegate>
to the header. Then set webView.delegate to yes;
Once you have it set as a delegate, then you can get the absoluteString and the requestURL, and override the method. With the absoluteString you can check for a substring, text, domain, basically anything you want! You override shouldStartLoadWithRequest or webViewDidFinishLoad:
-(void)webViewDidFinishLoad:(UIWebView *)webView
-(void)shouldStartLoadWithRequest :(UIWebView *)webView
That calls everytime any webpage is loaded.

Resources