opening a received url with safari - ios

I am receiving a url from a server request, I made a button , when pressed, safari must open and go to the link,
My code below:
- (IBAction)openFeedbackWebViewPresser:(id)sender {
NSString *feedbackUrl = self.getConfig.feedbackURL;
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: feedbackUrl]];
}
If I print the feedback url , its as below:
https://xxx.xxxxx.com/CRM/feedback#/1715171559ae979371687#/10306
I tried to use another way:
NSURL *url = [NSURL URLWithString:feedbackUrl];
[[UIApplication sharedApplication] openURL: url];
the url is returning nil, knowing that the feedbackUrl contains a url.
Any idea whats wrong?
Thanks

Might be your string url is containing any spaces or any other special characters that must be encoded. like below
NSString *feedbackUrl = [self.getConfig.feedbackURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: feedbackUrl]];
hope it helps.!!!

This might be happening because of the special characters that must be encoded. The following encoding works for me-
NSString *feedbackUrl = [self.getConfig.feedbackURL stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: feedbackUrl]];

This is the code to open the link in browser, Hope this help
- (IBAction)emailButton:(id)sender {
[[UIApplication sharedApplication]
openURL:[NSURL URLWithString:self.emailLabel.text]];
}

Related

Make a phone call from UIWebView without prompt

SCENARIO
I have an app that is a UIWebView, I make some url overriding for requirements.
PROBLEM
To make a call opening url with tel: works weird in iOS7 and iOS8, it makes the phone call direct in the background, but it also ask for the confirmation, so user experience is horrible:
[[UIApplication sharedApplication] openURL:request.URL];
SOLUTION
To solve this issue, I used telprompt. It works nice in all iOS versions:
NSURL *url = [NSURL URLWithString:#"telprompt://637****"];
return [[UIApplication sharedApplication] openURL:url];
But shows this confirmation dialog:
QUESTION
Now, I have a new requirement, to make the phone call without confirmation or prompt. So... There is some way to make a phone call in iOS omitting the confirmation prompt?
I want something like
NSURL *url = [NSURL URLWithString:#"telnoprompt://637******"];
return [[UIApplication sharedApplication] openURL:url];
NSMutableCharacterSet *characterSet =[NSMutableCharacterSet characterSetWithCharactersInString:#" "];
NSArray *arrayOfComponents = [phone_number componentsSeparatedByCharactersInSet:characterSet];
phone_number = [arrayOfComponents componentsJoinedByString:#""];
NSString *phoneURLString = [NSString stringWithFormat:#"tel:%#", phone_number];
NSString *escapedUrlString = [phoneURLString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *phoneURL = [NSURL URLWithString:escapedUrlString];

IOS IBAction button not going to website using URL

i have a button on a view controller and i want to click the button and it goes to a web site.
the website is held on parse.com.
the code as follows
- (IBAction)WebAddressBtn:(id)sender {
NSString *url = [self.exam objectForKey:#"Website"];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
NSLog(#"website: %#",url);
}
the NSLog shows Null for the value url
but the data is held at
self.exam objectForKey:#"Website"
NSLog confirms its there
this works and will go to google
- (IBAction)WebAddressBtn:(id)sender {
NSString *url = #"http://www.google.com";
//[self.exam objectForKey:#"Website"];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
NSLog(#"website: %#",url);
}
if i try
NSURL *url = [self.exam objectForKey:#"Website"];
url is still showing as Null
but i know the data is in self.exam objectForKey:#"Website
NSLog Output for data
name = "DMK Media & Photography Ltd";
phone1 = 01993835148;
phone2 = 07795966848;
postcode = "OX28 4BT";
products = "<PFRelation: 0x10dc75320>(<00000000 00000000>.(null) -> products)";
website = "http://www.dmkmedia.co.uk";
[UIApplication sharedApplication] openURL wont work properly if it doesnot have properly formatted url. Please check whether your url has "http://"

Sygic URL scheme - App does not open

I am trying to integrate the most popular navigation apps into my app, and all of those I chose work, except for Sygic.
Following this guide, I wrote the code:
NSString *URL = [NSString stringWithFormat:#"com.sygic.aura://coordinate|%f|%f|drive",
self.coordinate.longitude,
self.coordinate.latitude];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:URL]];
But when the code is run, Sygic doesn't open, nothing happens.
Checking [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:#"com.sygic.aura://"]] returns YES when the app is installed and NO when it's not (as it should).
I tested using "Sygic Brasil" and "Sygic (All Regions)", version 13, but neither will open.
I also tried percent-escaping the URL string, and that didn't work either.
you try following code,
NSString *URL = [NSString stringWithFormat:#"com.sygic.aura://coordinate|%f|%f|drive",
self.coordinate.longitude,
self.coordinate.latitude];
NSURL *newURL = [NSURL URLWithString:[URL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] ];
if(newURL)
{
[[UIApplication sharedApplication] openURL:newURL];
}
else
{
NSLog(#"Something wrong with lat or long or both");
}

Hyperlink from Plist in Xcode

I have a button in my app when clicked is supposed to take you to the website of the business which is stored in a plist. I can get the button to work with this code:
-(IBAction)search:(id)sender{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://www.google.com"]];
}
Now how can I manipulate this code to call the website from a plist already established.
Get the link string from the p-list.
NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"Info" ofType:#"plist"]];
NSString * urlString = dictionary[#"TheKeyPath"]; // theKeyPath referring to the key that you assigned to the url string in the p-list
Then very simple...
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
I wrote it like this but the button never appears to do anything, I wrote an NSLog and it does click the button but does not send to safari.
-(IBAction)search:(id)sender{
NSString *searchsite = [resultDic objectForKey:#"Search"];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:searchsite]];
}
What is confusing is I have a call function wrote like this and it works just fine.
-(IBAction)callPhone:(id)sender {
NSString* yourActualNumber = [NSString stringWithFormat:#"tel:%#",resultDic[#"Phone"]];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:yourActualNumber]];
}

launch youtube channel in youtube app

For launching video on youtube app, I am using below code.
NSURL *instagramURL = [NSURL URLWithString:#"youtube://foo"];
if ([[UIApplication sharedApplication] canOpenURL:instagramURL]) {
NSLog(#"opening youtube app...");
NSString *stringURL = #"http://www.youtube.com/watch?v=H9VdapQyWfg";
NSURL *url = [NSURL URLWithString:stringURL];
[[UIApplication sharedApplication] openURL:url];
} else {
// open in UIWebView in WebViewViewController
WebViewViewController *secondView = [self.storyboard instantiateViewControllerWithIdentifier:#"webinterface"];
secondView.headerLabel = #"YouTube";
secondView.webPath = #"http://www.youtube.com/watch?v=H9VdapQyWfg";
[self.navigationController pushViewController:secondView animated:YES];
}
Now client changed the mind and asking to put channel in iPhone app.
For testing, I used link http://www.youtube.com/user/richarddawkinsdotnet
BUT when I use this link, instead of youtube app, it always opens in SAFARI. :(
Any idea/ suggestion on how can I open channel in YouTube app with link provided?
You're code's going wrong because, although you're checking if you can open the youTube URL more or less correctly, you're then just opening a web address, which will always open in Safari.
This is the code I've just used, which is working for me. You might want to modify the else statement if you want to fallback to using a webviewcontroller as this will open Safari if the youtube app isn't installed.
NSString *channelName = #"TheNameOfTheChannel";
NSURL *linkToAppURL = [NSURL URLWithString:[NSString stringWithFormat:#"youtube://user/%#",channelName]];
NSURL *linkToWebURL = [NSURL URLWithString:[NSString stringWithFormat:#"http://www.youtube.com/user/%#",channelName]];
if ([[UIApplication sharedApplication] canOpenURL:linkToAppURL]) {
// Can open the youtube app URL so launch the youTube app with this URL
[[UIApplication sharedApplication] openURL:linkToAppURL];
}
else{
// Can't open the youtube app URL so launch Safari instead
[[UIApplication sharedApplication] openURL:linkToWebURL];
}
Same answer, but shorter:
if (![[UIApplication sharedApplication] openURL: [NSURL URLWithString: #"youtube://user/%channelName%"]]) {
NSURL *webURL = [NSURL URLWithString:#"http://www.youtube.com/user/%channelName%"];
[[UIApplication sharedApplication] openURL: webURL];
}
and twitter:
if (![[UIApplication sharedApplication] openURL: [NSURL URLWithString: #"twitter://user?screen_name=%channelName%"]]) {
NSURL *webURL = [NSURL URLWithString:#"http://twitter.com/%channelName%"];
[[UIApplication sharedApplication] openURL: webURL];
}
And here is a more exhaustive list of apps:
http://wiki.akosma.com/IPhone_URL_Schemes
youtube://user/<channel-id> stopped working in iOS 9 so i research and found this working well now in iOS 9
youtube://www.youtube.com/channel/<channel-id>

Resources