我不知道如何检查手机上是否安装了应用程序!或者当应用程序被安装时,打开该应用程序,否则打开Appstore链接以下载该应用程序。我正在使用 swift 3。我想使用应用程序名称或捆绑标识符来完成此操作。 谢谢!
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")
}
}
在查看了许多答案后,我编写了一个通用代码,可以帮助新用户。如果您有两个移动应用程序,例如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中编写方法以检查应用程序是否已安装在手机上!如果已安装,则打开该应用程序,否则打开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)
}
}
}
}
我希望它能帮助一些人。
看了其他答案和它们的评论,我得出这样的印象,即提问者想知道是否安装任何给定的应用程序。
从iOS 9.0开始,这是不可能的。
iOS 9及更高版本的应用程序必须在Info.plist中具有所请求的URL方案列表,才能使用canOpenURL:
。这是为了保护用户隐私,因为广告商正在以一种可以说是侵入性的方式滥用这个功能。(有关这些更改的更多详细信息,请参见这篇优秀文章。)
当然,该列表是静态的,在构建时或提交到App Store后无法更改。如果苹果不喜欢您选择的内容,则完全有权拒绝它。
恐怕,根据合理的要求,在iOS 9.0及更高版本中无法实现您正在询问的内容。
编辑:我还想明确一点,即应用程序的URL方案可能不一定与其名称匹配。(这更多是一个常量命名不当而不是功能问题。)曾经有一份包含每个URI方案文档的巨大列表,但令人痛心和恰当的是,托管它的wiki似乎已经关闭了。
在您当前的应用程序中,在info.plist文件中添加类型为Array的LSApplicationQueriesSchemes键。在其中输入您想要从应用程序打开的bundle id或Appname的条目。
其他应用程序应在该应用程序URL方案中具有其Bundle 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")
}
如果应用程序已安装,则会显示一个警报,其中包含 Appname,以便在应用程序中打开它。URL_scheme:// or Bundle_Id://
com.apple.mobilemail
一起使用。 - Mycroft Cannercom.apple.mobilemail
配合使用(不在模拟器中,需要一个设备)@MycroftCanner - Dasoga首先打开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")
}
<APP URL SCEHME>
: The URL Scheme of the app which you want to open<YOUR APP URL>
: The App Itunes URL
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)
}
}