How do I open another application with tvOS? - objective-c

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.

Related

Open settings programmatically in iOS

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.

Determining Ability of Device to Make Phone Call (including with Continuity)

I have been trying to determine whether a device is able to make a phone call, including with the new iOS9 Continuity features which allow iPads to make phone calls when connected to an iPhone. Presently my code looks something like this:
NSURL *call = [NSURL URLWithString:[NSString stringWithFormat:#"tel:1111111111"]];
if([[UIApplication sharedApplication] canOpenURL:call])
{
[[UIApplication sharedApplication] openURL:call];
}
else
{
//show an alert
}
However, when I run this on an iPad (whether or not the iPad has the phone call through an iPhone enabled) the canOpenURL function always returns true. Is there any way to distinguish whether an iPad or iOS device will make a phone call as opposed to whether it theoretically could (if a phone were connected) make the call?

YouTube URL Scheme tvOS

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

How to open Notification Center via URL Scheme from app?

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,

Url scheme : Open app or Appstore from mail

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