Recognise click on link within web page in iOS - ios

I want to load one URL in web view which is in my application. If user clicks on any URL on that web page then I want to open clicked URL in default safari app i.e. out of my application.
I know shouldStartLoadWithRequest: but it will get call whenever new URL starts loading even if it is loading an image in my web page.
How can I recognise that user clicked on URL on my web page?

You can check the navigationType parameter in webView:shouldStartLoadWithRequest:navigationType:
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
switch (navigationType) {
case UIWebViewNavigationTypeLinkClicked:
// user clicked on link
break;
case UIWebViewNavigationTypeOther:
// request was caused by an image that's being loaded
break;
}
return YES;
}
There are more UIWebViewNavigationTypes that you can use to determine what caused the request:
enum {
UIWebViewNavigationTypeLinkClicked,
UIWebViewNavigationTypeFormSubmitted,
UIWebViewNavigationTypeBackForward,
UIWebViewNavigationTypeReload,
UIWebViewNavigationTypeFormResubmitted,
UIWebViewNavigationTypeOther
};
typedef NSUInteger UIWebViewNavigationType;

Related

How can I get response from UIWebView page to an iOS app?

I'm using UIWebView as a subview in a UIViewController. What this WebView does is it allows user to submit a form and that form responses as JSON object. How can I get this response to an app? I'm not able to find what response comes. All I know is that response is JSON. I even tried using shouldStartLoadWithRequest(). But it only shows URL. Any help will be appreciated.
I use the 'window.location'. A little uncomfortable but fine.
You can try implementing this method may be it helps you
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
and check for the navigation types out of these
typedef NS_ENUM(NSInteger, UIWebViewNavigationType) {
UIWebViewNavigationTypeLinkClicked,
UIWebViewNavigationTypeFormSubmitted,
UIWebViewNavigationTypeBackForward,
UIWebViewNavigationTypeReload,
UIWebViewNavigationTypeFormResubmitted,
UIWebViewNavigationTypeOther
}

Access requested URL in webViewDidFinishLoad?

I'm very new to iOS development so please be gentle.
I understand that webViewDidFinishLoad will fire for each <iframe> plus the original html page.
I'm trying to find the URL of the request that resulted in that webViewDidFinishLoad. As far as I can tell, I can only access webView.request.mainDocumentURL.
I find this perplexing. Inside the shouldStartLoadWithRequest method, which fires also for each <iframe> and the original html request, you can access the URL in question, be it the iframe or the parent page.
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSLog(#"Parent page request: %#", request.mainDocumentURL);
NSLog(#"Actual URL request: %#", [request URL]); // This returns what I want.
return YES;
}
That first log statement will output the url.
However, inside webViewDidFinishLoad
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSURLRequest* request = [webView request];
NSLog(#"Parent page request: %#", request.mainDocumentURL);
NSLog(#"Request outputs the parent, not the iframe: %#", [request URL]);
// how do I access the <iframe> url?
}
Help me StackOverflow. My google-fu is weak and your wisdom is strong.
EDIT: I should be clear on the goal of this. I want to Do Stuff™ when the load event of the parent page fires, not for iframes. I want this to happen as soon as possible, so I don't want to wait until everything finishes.
Think of the UIWebView as your browser. The scope of the object you are interrogating is at the browser level, the iFrames are more granular than that. The URL that populates the iFrame is used to stream content for that section of the page, but for the purposes of the UIWebView, you only need to remember the main URL so the page can be managed (reloaded, etc...) as that URL clearly brings with it instructions as to how to manage the iFrame's content.
If you really need to those iFrame source URLS, simply extendeding UIWebView to hold an array of links (populate the array inside "shouldStartLoadWithRequest"). You can then do whatever you want with that list of links.
Good Luck.

How handle cancel button of facebook dialog apprequest?

As you know, you can display some facebook views to make some operations, like app requests, post etc. There is a delegate to manage the callbacks like : dialogDidComplete:, dialogDidNotComplete:. The view is like that :
But there is not differentiation between the cancel and share button. You will have the same callback in dialogDidComplete:. The only way to manage a cancelation is the little cross in the corner.
In my case I would like to do some operations if the user pressed share and not when he pressed Cancel.
The private social network Path manage this case and I'm wondering how ?
Do you guys have some ideas ?
If I am not wrong:
if you press Share you get a requestID back as part of the returned URL
if you press Cancel you are returned into dialogDidComplete but it does not return a requestID back as part of the returned URL.
Code:
- (void)dialogCompleteWithUrl:(NSURL *)url {
if (![url query]) {
NSLog(#"User canceled dialog or there was an error");
return;
}
}

Phonegap 2.0 , Cordova external links

I have a situation and I spend so much time in google with no success.
I want to open in my app (IOS), external links which are like that
"External Link" to open in safari not web view. where I have set up in "Cordova.plist"
OpenAllWhitelistURLsInWebView : true
Because I hav as well some Iframe inside my app, where I want to keep user in web view and not to leave the app.
An I have no idea why target="_blank" doesn't work, where here :
https://build.phonegap.com/blog/access-tags it says:
"
on iOS, if a domain is whitelisted, a link will take over the entire webview, unless the link's target is _blank, in which case it will open in the browser. If it is not, it will log an error on the device, while doing nothing from the user's perspective.
"
I tried to use JS way as well,
window.open('http://www.google.com', '_blank');
with no success :(
PS: I do have all my links in External host set up
I appreciate any help.
Thanks!
What you need is this charmer in your MainViewController.m
- (BOOL)webView:(UIWebView *)theWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *url = [request URL];
// Intercept the external http requests and forward to Safari.app
// Otherwise forward to the PhoneGap WebView
if ([[url scheme] isEqualToString:#"http"] || [[url scheme] isEqualToString:#"https"]) {
[[UIApplication sharedApplication] openURL:url];
return NO;
}
else {
return [ super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType ];
}
}
It works with me using following setup:
Cordova.plist:
OpenAllWhitelistURLsInWebView: false
external Hosts: google.com
Link in Code:
< a target='_blank' href='http://maps.google.com/maps?q=something'>
Hope it works for you as well :)

jQuery: Continue custom click event on successful ajax login

I have a set of links (add to wishlist) with click event . The click event checks if the user is logged in (ajax request) and depending on the result the click continues. In the case the user is not logged in I have a generic jQuery UI modal dialog that the user can use to log in.
That all works well but I would like to extend the functionality to be able to continue with click of the links after the user logs in using the dialog.
ie. CLICK ADD TO WISHLIST -> LOGGED IN ?
YES -> CONTINUE LINK CLICK (add to wishlist)
NO -> SHOW LOGIN DIALOG ->
USER SUBMITS CREDENTIALS -> LOGGED IN?
YES => CONTINUE LINK CLICK (add to
wishlist)
NO => SHOW LOGIN ERROR
Of course I do not want to perform any postbacks, all through ajax and jquery.
Thanks
You could pass the url of the click action to the login modal and redirect after successful login.
Alternativly, if you have set it up to login via AJAX, you could store the click event target and then trigger a click event after a successful login.
Something like the following:
var is_logged_in = false;
$('a.add_to_wishlist').click(function(e) {
if(is_logged_in) {
//do stuff
} else {
show_modal(e.target); //pass the event target to the modal
}
});
function show_modal(trigger) {
//do stuff
is_logged_in = true;
$(trigger).trigger('click');
}
Hopefully this helps.
If you're in control of your server side code then you should place the authentication + save to wish list in the same request.
User clicks add to wish list
Ajax request to add page to wish list is sent to server.
Server check to see if user is logged in.
If the user is logged in: save the item to the wish list and return a success response.
If the user it not logged in: return a failed response.
The ajax response callback should update the UI based on the response.
If you're not in control of your server code then you'll have to make two ajax request if the authentication request is successful.

Resources