【发布时间】:2017-01-09 09:58:38
【问题描述】:
我不知道如何检查手机上是否安装了应用程序!或者在安装了 App 后,打开该应用程序,否则打开 Appstore 链接下载该应用程序。我正在使用 swift 3。我想使用 app name 或 bundle identifier 来完成。 谢谢!
【问题讨论】:
-
如何在应用中添加 URLScheme 并获取结果
我不知道如何检查手机上是否安装了应用程序!或者在安装了 App 后,打开该应用程序,否则打开 Appstore 链接下载该应用程序。我正在使用 swift 3。我想使用 app name 或 bundle identifier 来完成。 谢谢!
【问题讨论】:
func openApp(appName:String) {
let appName = "instagram"
let appScheme = "\(appName)://app"
let appUrl = URL(string: appScheme)
if UIApplication.shared.canOpenURL(appUrl! as URL) {
UIApplication.shared.open(appUrl!)
} else {
print("App not installed")
}
}
【讨论】:
在其他答案和他们的 cmets 之间,我得到的印象是提问者希望能够查看是否安装了任何给定的应用程序。
从 iOS 9.0 开始,这是不可能的。
iOS 9 及更高版本的应用程序必须在 Info.plist 中包含请求的 URL 方案列表,然后才能使用 canOpenURL:。这是为了保护用户隐私,因为广告商以一种可以说是侵入性的方式滥用此功能。 (有关这些更改的更多详细信息,请参阅this excellent post。)
当然,该列表是静态的,在构建时间或提交到 App Store 后无法更改。如果 Apple 不喜欢你选择的那些,他们完全有权拒绝。
恐怕您的要求在 iOS 9.0 及更高版本的合理范围内是不可能的。
编辑:我还想明确一点,应用的 URL 方案不一定与其名称匹配得很好。 (这与其说是功能问题,不如说是一个命名错误的常量的问题。)曾经有一个巨大的已知 URI 方案列表,其中每个方案都有文档,但令人痛心且恰如其分的是,托管它的 wiki 似乎已经关闭.
【讨论】:
在查看了这么多答案之后,我正在编写一个通用代码,这将对新用户有所帮助。如果您有两个移动应用 App1 和 App2,如果您想在 App2 中检查 App1 是否已安装在他的设备中,下面是代码。
在 App1 中将这个属性添加到 info.plist 文件中
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLName</key>
<string>com.companyName.App1</string>
<key>CFBundleURLSchemes</key>
<array>
<string>App1</string>
</array>
</dict>
</array>
在 App2 中将这个属性添加到 info.plist 文件中
<key>LSApplicationQueriesSchemes</key>
<array>
<string>App1</string>
</array>
在 App2 中编写方法来检查手机是否安装了应用程序!或者在安装了App后打开应用,否则打开Appstore链接下载应用如下。
func checkAndOpenApp(){
let app = UIApplication.shared
let appScheme = "App1://app"
if app.canOpenURL(URL(string: appScheme)!) {
print("App is install and can be opened")
let url = URL(string:appScheme)!
if #available(iOS 10.0, *) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(url)
}
} else {
print("App in not installed. Go to AppStore")
if let url = URL(string: "https://apps.apple.com/us/app/App1/id1445847940?ls=1"),
UIApplication.shared.canOpenURL(url)
{
if #available(iOS 10.0, *) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(url)
}
}
}
}
希望对大家有所帮助。
【讨论】:
斯威夫特 4.1。一位开发者可以在 AppStore 上拥有多个应用程序。因此,我需要检查用户是否安装了同一开发人员的其他应用程序。我有其他应用程序的 Bundle ID。虽然您可以使用 Appname 代替 Bundle Id。所以我按照以下步骤操作。
在您当前的应用程序中,在 info.plist 文件中添加 Array 类型的 LSApplicationQueriesSchemes 键。输入您想从您的应用打开的应用的捆绑包 ID 或应用名称。
其他应用应在该应用 URL 方案中包含其捆绑包 ID 或 Appname 条目。
let app = UIApplication.shared let bundleID = "some.Bundle.Id://" if app.canOpenURL(URL(string: bundleID)!) { print("App is install and can be opened") let url = URL(string:bundleID)! if #available(iOS 10.0, *) { UIApplication.shared.open(url, options: [:], completionHandler: nil) } else { UIApplication.shared.openURL(url) } } else { print("App in not installed. Go to AppStore") }
URL_scheme:// or Bundle_Id://
如果安装了应用程序,它将显示带有 Appname 的警报以在应用程序中打开它。
【讨论】:
com.apple.mobilemail
com.apple.mobilemail 一起工作(不在模拟器中,你需要一个设备)@MycroftCanner
这对我有用(Swift 3.0)
应提供以下两个输入:
<APP URL SCEHME>: 你要打开的应用的 URL Scheme<YOUR APP URL>:App Itunes 网址
func openApp() {
let appURL = NSURL(string: "<APP URL SCHEME>")
if UIApplication.shared.canOpenURL(appURL as! URL) {
print("Opening App...")
}else {
UIApplication.shared.openURL(NSURL(string: "<YOUR APP URL>")! as URL)
}
}
【讨论】:
首先转到 info.plist,添加 LSApplicationQueriesSchemes 添加一个项目并将 instagram 放在该项目上。现在这段代码将完美运行。
let appName = "instagram"
let appScheme = "\(appName)://"
let appUrl = URL(string: appScheme)
if UIApplication.shared.canOpenURL(appUrl! as URL)
{
UIApplication.shared.open(appUrl!)
} else {
print("App not installed")
}
【讨论】: