Phonegap 2.0 , Cordova external links - ios

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 :)

Related

Error logging into Instagram in iOS app using UIWebView

I'm following Instagram authentication [recommended] steps using UIWebView on an iOS app.
After entering credentials, hitting login loads a page with following error.
This page could not be loaded. If you have cookies disabled in your
browser, or you are browsing in private mode, please try enabling
cookies or turning off private mode, and then retrying your action.
And, this only happens on first run through the steps of authentication; on the next attempt, everything works smooth as silk. I get the code suffixed to redirect url and I request access token using it.
Screenshot:
There's already another question here and it doesn't help.
EDIT:
It seems like Cookies issue. Though, I haven't been able to fix it yet.
I had a similar problem when I was deleting cookies to make sure the log in screen appeared and not just using the currently logged in user. Try (swift):
let storage = HTTPCookieStorage.shared
storage.cookieAcceptPolicy = .always
I managed to solve this problem about an hour ago.
First, DON'T use UIWebView or WebView; use a WKWebView instead.
You see, you need to implement the method
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error
when instagram's login page forwards you to your redirect_url , it FAILS to navigate. Probably because we're not doing the server-side auth, but only the client-side auth, and the redirect_url is not a valid url.
We're expecting the page to REDIRECT to an invalid url, but when that happens, the WKWebView doesn't call the method
- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation
or the method
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation
So. What you need to do is focus on that first method, check the error variable, and extract error.userInfo.
In my case, this is how I solved the problem:
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error
{
NSURL *err = [error.userInfo objectForKey:#"NSErrorFailingURLKey"];
if([err.absoluteString hasPrefix:INSTA_REDIRECT_URL]){
NSString *token = [[err.absoluteString componentsSeparatedByString:#"#access_token="] objectAtIndex:1];
[Utils saveToken:token];
[self.webView setHidden:YES];
//open next view controller
}else{
//TODO: No internet? something else went wrong..
NSLog(#"Error: %#", error);
}
}
Hope it helps.
Best of luck! ;)

how to pass default URL into default browser in IOS?

I have pass this below code in viewDidLoad method but it take the https:// not the the passed URL so i am confused please tell me the solution of my problem if anybody can know.
-(void)viewDidLoad
{
[super viewDidLoad];
NSString *customURL = #"http://www.dannoshottips.com/";
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:customURL]];
}
not required for add https:// in url because your site is not use https:// protocol.
Your site www.dannoshottips.com not https protocol used.
this site not secure protocol used so you can not use https://.
http://www.dannoshottips.com/
1: http://www.dannoshottips.com/ is open in browser
https://www.dannoshottips.com/ is not open in browser.

Recognise click on link within web page in 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;

how to launch safari even when the url is not valid

I know how to launch safari using the:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://www.google.com"]];
But, this method returns false when the url is not valid, and nothing happens. So, I'd like to launch safari even when the url is invalid. Is it possible?
NO it is not possible to open URL (which is invalid) with safari or any other bowser in iOS or another OS, So it's better to make valid URL rather then fighting with it.
Using below code check, if the URL is valid or not.
NSURL *candidateURL = [NSURL URLWithString:candidate];
if (candidateURL && candidateURL.scheme && candidateURL.host)
{
//Open that URL using openURL method...
}
else
{
//Open any valid hardcoded URL using openURL method
}
Short answer? No.
Long answer? Technically No. But there is a workaround. If you use a redirection service/url shortener, you can hide your invalid url. For example, try this url: http://goo.gl/zRci0B
Safari will be able to open the link but it wont go anywhere. So what ever url(valid/invalid) you want to open, always wrap it with a redirection service.

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.

Resources