问题描述
UIApplication:openURL
有效吗?
NSString *iTunesLink = @"http://www.youtube.com/watch?v=TFFkK2SmPg4";
BOOL did = [[UIApplication sharedApplication] openURL:[NSURL URLWithString:iTunesLink]];
这没有任何作用.
推荐答案
我假设你想测试一个 自定义 URL 方案.您将需要使用 canOpenURL
来查看 URL 是否可以先打开.canOpenURL
返回一个 BOOL
值,指示 URL 的方案是否可以被安装在设备上的某些应用程序处理.如果 canOpenURL
返回 YES
,那么您将继续使用 openURL
打开 URL.
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 链接默认在 iOS 设备上打开 YouTube 应用.此行为尚无法在新 Apple TV 上进行测试,因为 YouTube 应用无法在 tvOS 测试版中访问.
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.
以下是如何使用 canOpenURL
查看 Facebook 是否使用其自定义 URL 方案安装在 iOS 设备上的示例:
Here's an example of how to use canOpenURL
to see if Facebook is installed on an iOS device using its Custom URL Scheme:
对象-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
}
斯威夫特:
// 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
}
我预计 Facebook 等应用程序将采用与 iOS 应用程序相同的方式实现其自定义 URL 方案.
I'd anticipate that applications such as Facebook, and others, will implement their Custom URL Schemes in the same manner as their iOS counterparts.
这篇关于如何使用 tvOS 打开另一个应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!