how to launch safari even when the url is not valid - ios

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.

Related

objective c - Open with suggestions when trying to open URL schemes

I started an application that can handle SMS or Mail url schemes but I don't want to open the default apps installed. I have tried doing this
[[UIApplication sharedApplication] openURL: url];
but this opens the default SMS and Mail apps.
I tried using the UIActivityViewController
NSString *url=#"mailto:sample#gmail.com";
NSURL *schemeURL = [NSURL URLWithString:url];
NSString * title =[NSString stringWithFormat:#"Send Email",url];
NSArray* dataToShare = #[url];
UIActivityViewController* activityViewController =[[UIActivityViewController alloc] initWithActivityItems:dataToShare applicationActivities:nil]
but this doesn't prefill the recipient field with the specified email.
It would be nice if the behaviour is the same with UIActivityViewController but let's us prefill information such as recipient.
Any suggestions ?
You can use non-default apps on macos but I don't think you can do so on iOS. Have a look at e.g. https://developer.apple.com/documentation/uikit/uiapplication/1648685-openurl?language=objc .
You could try with a URL similar to below to send with a given subject and body.
#"message://mailto://sample#gmail.com?subject=Subject&body=Body"
I think if you drop the message bit it will still work. I use it like that inside a HTML page inside an app somewhere.
If you need any special characters you will have to encode them in the subject and body.

Silence "-canOpenURL: failed for URL:"

I have added my URL schemes to Info.plist, as required by iOS 9. However, calls result in:
-canOpenURL: failed for URL: "scheme://" - error: "(null)"
being logged to the console. The calls are succeeding and returning the correct value, but these log messages are annoying. How can I disable them?
Try just using openURL: to check if it can open, since it returns a Bool, then call openURL: again:
if let url = NSURL(string: keyword) {
if UIApplication.sharedApplication().openURL(url) {
UIApplication.sharedApplication().openURL(url)
}
}
Looks weird to see the same statement repeated twice, but at least it doesn't spit out a failure message like canOpenURL: does. If anyone knows how to make it look less weird, please do tell.

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.

How to open an url from the browser with a parameter in ios

I want to open an URL from the browser in ios. I know how to open an normal url.. but here I want to pass a parameter to the url.... This is what I used to open the url from the browser
NSURL *url = [NSURL URLWithString:#"http://www.iphonedevelopertips.com"];
[[UIApplication sharedApplication] openURL:url];
Then how can I modify this according to pass a parameter
Thanks
here i pass the country name. you can pass anything what you want.
CountryName=#"India";
NSURL * url=[NSURL URLWithString:[NSString stringWithFormat:#"http://www.webservicex.net/globalweather.asmx/GetCitiesByCountry?CountryName=%#",CountryName]];
do like this pass whatever you want pass like this.

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

Resources