How can I open the link on an application? [duplicate] - ios

This question already has answers here:
How can I launch Safari from an iPhone app?
(7 answers)
Closed 7 years ago.
I added a code for Xcode from the internet.
This one:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://www.youtube.com"]];
It took me to www.youtube.com on the Safari browser, what if I have youtube application? how can it open that from the application in my iPhone?
p.s I’m a newbie

- (IBAction)btnYoutube:(id)sender {
NSURL *linkToAppURL = [NSURL URLWithString:[NSString stringWithFormat:#"youtube://user/%#",#"toyotaleasing"]];
NSURL *linkToWebURL = [NSURL URLWithString:[NSString stringWithFormat:#"http://www.youtube.com/user/%#",#"toyotaleasing"]];
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];
}
}

Use URL scheme for youtube : youtube://
Eg.
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:#"youtube://www.youtube.com/watch?v=<video-id>"]]) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"youtube://www.youtube.com/watch?v=<video-id>"]];
}
Hope, it'll help you.

Related

openUrl is not redirecting to safari in iOS 8.3

I have my old application which is working fine till iOS 8.2. In one button action I have to open safari using openUrl and this is working fine for the device with iOS 8.2. But when I run it in iOS 8.3 then its not opening the safari and when I tried to print it then its returning NO.
Is there any change in iOS 8.3 for openUrl method?
Edit 1:
NSString *loginUrl=[object sighnUpUrl];
NSLog(#"%d",[[UIApplication sharedApplication] openURL:[NSURL URLWithString:loginUrl]]);
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:loginUrl]];
I am just using the url of login page of my web.
Edit 2:
NSURL *url = [NSURL URLWithString:#"http://www.stackoverflow.com"];
if (![[UIApplication sharedApplication] openURL:url]) {
NSLog(#"%#%#",#"Failed to open url:",[url description]);
}
I tried the same code and getting Failed to open url:http://www.stackoverflow.com.
Edit 3:
NSURL *URL = [NSURL URLWithString:#"http://www.google.de"];
if ([[UIApplication sharedApplication] canOpenURL:URL]) {
[[UIApplication sharedApplication] openURL:URL];
}
When I tried the same above code then its entering the if block but still unable to the safari.
Note: All the above Codes are working in iOS 8.2.
this is the way to go:
NSURL *URL = [NSURL URLWithString:#"http://www.google.de"];
if ([[UIApplication sharedApplication] canOpenURL:URL]) {
[[UIApplication sharedApplication] openURL:URL];
}
Check this
NSURL *url = [NSURL URLWithString:#"http://www.stackoverflow.com"];
if (![[UIApplication sharedApplication] openURL:url]) {
NSLog(#"%#%#",#"Failed to open url:",[url description]);
}
Please try this:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://www.google.com"]];
Edit:
BOOL canOpenURL = [[UIApplication sharedApplication] canOpenURL:yourURL];
Try to place your URL here & check whether it returns True or False.
I have gone through all the answers and I know all of them will work.The issue was on the other side.I am using Mobile Device Management in my app.Some one from the server side had disabled safari using MDM.Unfortunately for the device with iOS 8.2 it was enabled and for the devices with iOS 8.3 it was disabled which made me think its an iOS issue.So while safari is disabled using MDM it can't open any url.My Bad.Thanks for all your answers.
ios8.3 .apk file received by e-mail, can not open other applications, it may be a BUG ios8.3 of

can we force to open facebook link in browser not in native app iphone

Is it possible to force through your application that it will open the web link in safari only not in native app like facebook, twitter or web view etc.
So far i searched found only this code that is used for opening the link
NSURL *url = [NSURL URLWithString:#"www.facebook.com"];
[[UIApplication sharedApplication] openURL:url];
But what can we paste in else there below?
if ([[UIApplication sharedApplication] canOpenURL:nsurl]){
[[UIApplication sharedApplication] openURL:nsurl];
}
else {
//Open the url as usual, but how?
}
Use http://facebook.com/ instead of www.facebook.com
this will force a link to open in Safari instead of native app*
NSURL *myURL = [NSURL URLWithString:#"http://facebook.com/"];
if ([[UIApplication sharedApplication] canOpenURL:myURL]) {
[[UIApplication sharedApplication] openURL:myURL];
}
You can use a webView to open any link of your choice.

Facebook App does not display page when linked from iOS App

I'm working on iOS application. Here is the code i used
NSURL *url = [NSURL URLWithString:#"fb://page/5718758966"];
if ([[UIApplication sharedApplication] canOpenURL:url])
{
[[UIApplication sharedApplication] openURL:url];
}
else
{
//Open the url as usual
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"https://facebook.com/5718758966"]];
}
As I mentioned, this code has worked to open Facebook app but shows blank page; my question is if there is an alternative link I can use that will both open the app and direct the user to this specific page, or is this simply a Facebook bug?
Thanks!
Instead of "fb://page/5718758966" try "fb://profile/5718758966" for your URL.

open Facebook page from the app

I was trying to open the Facebook app with particular Facebook page from my app with the following code:
NSURL *url = [NSURL URLWithString:#"http://www.facebook.com/pagename"];
[[UIApplication sharedApplication] openURL:url];
When I click on the button it opens the Facebook app but the required page doesn't load or show.
Is there any other way to open the Facebook app with particular Facebook page? Please correct me If I am doing something wrong.
Thanks in advance.
To open the facebook app directly you should do the following:
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString: #"fb://profile/PAGE_ID"]])
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: #"fb://profile/PAGE_ID"]];
}
else
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: #"http://facebook.com/PAGE_NAME"]];
}
This is a solution including a fallback for web.
To determine your PAGE_ID you could use a service like this.

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