以编程方式检查应用程序是否已安装

6
我想知道是否有Swift等效的以下Objective-C代码。
NSURL *appURL = [NSURL URLWithString: @"myapp://"];
if ([app canOpenURL: appURL] {
 NSLog(@"The app is this URL Scheme is installed on the device");
}
2个回答

15

在阅读本答案之前,您必须庄严承诺不执行您链接的页面上任何活动。(寻找约会应用程序?认真吗?)

方法基本相同:

if let appURL = NSURL(string: "myapp://test/url/") {
    let canOpen = UIApplication.sharedApplication().canOpenURL(appURL)
    println("Can open \"\(appURL)\": \(canOpen)")
}

这里有一个小改动:如果 UIApplication.sharedApplication().canOpenURL(uberURLScheme!) { let canOpen = UIApplication.sharedApplication().openURL(url!) } - Romain
请注意,sharedApplication 已被重命名为 shared,并且不再是一个函数。 - JCutting8

5

让我们假设有两个名为A和B的应用程序。

现在我想从应用程序A打开应用程序B,因此首先在应用程序B中创建URLSchema。

然后将以下代码添加到应用程序A的info.plist文件中。

<key>LSApplicationQueriesSchemes</key>
<array>
    <string>app b schema Name</string>
</array>

现在将以下代码添加到您想要打开应用程序B的应用程序A中。
UIApplication.shared.canOpenURL(URL(string:"app b schema Name://")! as URL)

谢谢。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接