I am trying to open YouTube's app from my application with the URL scheme or the YouTube.com domain which opens YouTube's app directly on an iOS device.
This is the code I tried:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:#"youtube://results?search_query=trailer+%#",movieTitle]]];
and
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:#"https://www.youtube.com/results?search_query=trailer+%#",movieTitle]]];
But nothing seems to work. Any ideas on how to retrieve the URL scheme for YouTube's tvOS application?
On Apple TV you can use this URL scheme to play a YouTube video in the YouTube app:
youtube://watch/video_id
Code example:
func playVideoInYouTube(_ identifier: String) {
if let url = URL(string: "youtube://watch/" + identifier),
UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
else {
// Inform the user about missing YouTube app
}
}
To use the canOpenURL method, you have to add the scheme to the Info.plist file under key LSApplicationQueriesSchemes, as part of a schemes array. In XML it looks like this:
<key>LSApplicationQueriesSchemes</key>
<array>
<string>youtube</string>
</array>
Not necessarily positing this as a “correct" answer, but I think all YouTube links, on iOS at least, are handled by http, not something like youtube://.
Source (looks to be from June 2015):
https://developer.apple.com/library/ios/featuredarticles/iPhoneURLScheme_Reference/YouTubeLinks/YouTubeLinks.html
I also encountered some issues playing youtube videos in the youtube app but I found a workaround, I used the XCDYouTubeKit library to play the youtube video's directly in my app. It's very easy to use and works fine so far.
XCDYouTubeKit : https://github.com/0xced/XCDYouTubeKit
Related
I am opening the device settings page from my app.When I open through iPhone X(iOS11) and iPhone 8 it goes to app settings . But when I open through iPhone 8 Plus and iPhone SE it goes to home settings page.
I need to open home settings page of iPhone like iPhone 8 Plus(open home settings page) . I am running the below code.
UIApplication *application = [UIApplication sharedApplication];
NSURL *settingsURL = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
[application openURL:settingsURL options:#{} completionHandler:^(BOOL success) {
if (success) {
NSLog(#"Opened url");
}
}];
My problem is, I created a new project and I ran this above code . Its working fine.And I can able to navigate to device home settings page in same iPhone X.
But when I run this code in my project in iPhone X it goes to app settings screen.I don't know where I missed.
Please help me if possible.
Thanks in advance.
Global Variable
UIApplicationOpenSettingsURLString
Used to create a URL that you can pass to the openURL: method. When you open the URL built from this string, the system launches the Settings app and displays the app’s custom settings, if it has any.
https://developer.apple.com/documentation/uikit/uiapplicationopensettingsurlstring?language=objc
This is from Apple official document. My guess is your app have its own setting page, that's why openURL function takes you to app's setting page instead of Setting home.
Does UIApplication:openURL work?
NSString *iTunesLink = #"http://www.youtube.com/watch?v=TFFkK2SmPg4";
BOOL did = [[UIApplication sharedApplication] openURL:[NSURL URLWithString:iTunesLink]];
This does nothing.
I assume you're wanting to test a Custom URL Scheme. You will want to use canOpenURL to see if the URL can be opened first. canOpenURL returns a BOOL value indicating whether or not the URL’s scheme can be handled by some app installed on the device. If canOpenURL returns YES then you would continue to open the URL with openURL.
YouTube links open the YouTube app by default on iOS devices. This behavior is not yet testable on the new Apple TV as YouTube's app is not accessible in the tvOS beta.
Here's an example of how to use canOpenURL to see if Facebook is installed on an iOS device using its Custom URL Scheme:
Obj-C:
// Check if FB app installed on device
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:#"fb://"]]) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"fb://profile/355356557838717"]];
}
else {
// FB not installed
// Do something else
}
Swift:
// Check if FB app installed on device
if UIApplication.sharedApplication().canOpenURL(NSURL(string:"fb://")!) {
UIApplication.sharedApplication().openURL(NSURL(string:"fb://profile/355356557838717")!)
}
else {
// FB not installed
// Do something else
}
I'd anticipate that applications such as Facebook, and others, will implement their Custom URL Schemes in the same manner as their iOS counterparts.
Using the URL Scheme below, I'm able to open the 'Settings' App from the app I'm currently working on.
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
Is it also possible to tweak the URL Scheme so that I can directly open the 'Notification Center' feature inside the 'Settings' App?
Kindly advise. All help appreciated!
I tried this code to prevent crashes from iOS versions and it's working for me:
if ([[UIApplication sharedApplication] respondsToSelector:#selector(openURL:options:completionHandler:)]) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString] options:#{} completionHandler:nil];
} else {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
}
tested on iOS 8 up;
you can try this url scheme: "prefs:root=NOTIFICATIONS_ID"
But u must add URL types in ur project: Click on ur Project in Project Navigator-> click on TARGET -> tab Info -> and add URL Types by click (+) and add URL scheme : "prefs".
if you still not clear check this Answer here
All the best,
I'm creating an application where users can share data by sending emails to each other.
In this mail there is a link to my app (I created an URL scheme for my app)
But, if the users doesn't have my application on his iPhone, then when he clicks on the link, nothing happens.
My question : How could I do to launch my app when the application is installed on the user's iPhone and launch the Appstore or a website if this application is not installed
Thank you
canOpenURL is what you're looking for.
Here is an example for launching twitter app:
NSURL* twitterURL = [NSURL URLWithString:[NSString stringWithFormat:#"twitter:#%#", twitterUser]];
if ([[UIApplication sharedApplication] canOpenURL:twitterURL]) {
[[UIApplication sharedApplication] openURL:twitterURL];
} else {
NSURL* url = [NSURL URLWithString:#"http://itunes.apple.com/us/app/twitter/id333903271?mt=8"];
[[UIApplication sharedApplication] openURL:url];
}
EDIT
If you want do do this from a webpage or html email, you find your answer here:
https://stackoverflow.com/a/627942/550177
and here:
https://stackoverflow.com/a/1109200/550177
Ok this may seem like a duplicate but I've been through all of the questions that center around the issue and I still can't seem to get this to work for my app.
I have followed the iOS tutorial on the Facebook Developer site and I have done the following:
Created a Facebook App to get the Facebook App ID.
I've assigned this App ID to a constant and I use it to initialize the Facebook object within my app.
I've made sure my app is flagged for multitasking
I've setup the fbAPP_ID in the plist under the URLs drop down
I've made my AppDelegate a FBSessionDelegate and implemented very simple fbDidLogin and fbDidNotLogin delegate methods.
Versions: XCode 4.3.2 on Lion, iOS 5.1, Facebook iOS 4.1.1
All of this and I still can't seem to get Facebook to return control back to my app when it authorizes. It just stays in Facebook instead.
Code: (Note FB_APP_ID changed to basic number for viewing here)
AppDelegate.h:
#import <UIKit/UIKit.h>
#import "FBConnect.h"
#define FB_APP_ID #"1234567890"
#interface AppDelegate : UIResponder <UIApplicationDelegate, FBSessionDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (nonatomic, strong) Facebook* facebook;
#end
AppDelegate.m:
#import "AppDelegate.h"
#implementation AppDelegate
#synthesize window = _window;
#synthesize facebook = _facebook;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
_facebook = [[Facebook alloc] initWithAppId:FB_APP_ID andDelegate:self];
if (![_facebook isSessionValid])
{
[_facebook authorize:nil];
}
}
- (BOOL)application:(UIApplication*)application handleOpenURL:(NSURL *)url
{
return [_facebook handleOpenURL:url];
}
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
return [_facebook handleOpenURL:url];
}
- (void)fbDidLogin
{
NSLog(#"App logged in");
}
- (void)fbDidNotLogin:(BOOL)cancelled
{
NSLog(#"App did not login");
}
#end
Relevant Info.plist porton:
Facebook App screen (mostly pinked out):
Note: I tried it with only basic settings and also with the iOS Native app settings with a fake iTunes store ID but the same bundle ID from my provisioning profile.
No matter what I do it always loads up Facebook and asks to authorize my app and then it simply closes the auth screen window and sits there on my Facebook screen instead of returning to my app.
Also note: I know it will always ask because I'm not checking for the auth token and expiration date.....that's the next step....for now I want to at least get it to return to my app before I worry about that.
Have I missed something here? Anything else I can try to get Facebook to return to my app?
EDIT: Also when debugging, I can't seem to find any spot where either openURL is called for some reason.
EDIT #2: When running it on the simulator, it loads up the FB Dialog via Safari (as expected) and then when I authorize, safari throws an error as if the URL they are trying to use to get back to my app is invalid:
This still doesn't tell me much since my fb1234567890 is setup right as far as I know in the Info.plist.
Your info.plist seems to be formatted incorrectly
Ensure that the url scheme is nested under an item (an array to be exact) called "URL Schemes"
This array should contain all possible URL Schemes that can launch your app
See the attached image for a better explanation of URL Schemes
Also, the "URL identifier" should be your bundle identifier: ex. com.mycompany.myapp
This should match the identifier you provide on your provisioning profiles
hope this helps!
All you have to follow these steps
STEP 1:
Facebook developer side stuff:
If your app is already submitted then fill iphone/ipad appstore id otherwise leave it empty.
STEP 2:
Next in your XCode:
Goto
Target > Info > URL Types > click plus icon and add the items like this-
In my application, there were both options to login via facebook and google so I created two URL Types. You can see second one are Facebook URL Type settings. URL Scheme will be your facebook app id from facebook Developer account with fb at starts. So if your facebook developer app id is 9876543267575645 then URL Scheme will become fb9876543267575645.
STEP 3:
Next, you have to add a new key in info.plist. So remain in same screen and add FacebookAppID like this screen:
All this stuff will bring your app back after successful facebook oauth.
STEP 4:
Rest you will do in your AppDelegate class. Simply add this method in AppDelegate.m
NSString *retURL = [NSString stringWithFormat:#"%#", url];
if ([retURL rangeOfString:facebookid].location != NSNotFound)
{
// if your application is coming back from facebook native app.
return [_facebook handleOpenURL:url];
}
else
{
// if your app is coming back from google plus oauth.
return [GPPURLHandler handleURL:url sourceApplication:sourceApplication annotation:annotation];
}
during all this, must intialise and synthesize facebook instance in AppDelegate.m
#synthesize facebook = _facebook;
and didFinishLaunchingWithOptions
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
_facebook = [[Facebook alloc] initWithAppId:facebookid];
// rest of your code is already here......
}
One more important thing. During I was implementing these steps, I also did too much research to achieve this and finally the working stuff is here. I made these settings in two of my apps.
But while I was creating third app, I started from beginning and did not follow STEP 1 and STEP 3 and voila.... redirection works! So i don't think facebook developer account Native iOS App settings are necessary while you are making Facebook login in your app..
I ran into another issue. I edited the raw Info.plist file and added:
<key>URL Types</key>
<array>
<dict>
<key>URL Schemes</key>
<array>
<string>fbxxxxxxxxx</string>
</array>
</dict>
</array>
That didn't work because the keys are not 'URL Types', 'URL Schemes' and 'URL Identifier', it's 'CFBundleURLTypes' , 'CFBundleURLSchemes' and 'CFBundleURLIdentifier' instead. Xcode will rename that in the editor to 'URL Types', 'URL Schemes' and 'URL Identifier'.
To be sure select the Info.plist in xcode, right click->show raw keys/values.
None of the above, or other answers on SO, worked for me, though I seemed to have the exact same issue.
However, when I enabled "Deep Linking", it worked perfectly, so it may have been a combination of all of the above AND enabling that option too.