ios 如何检查手机中是否安装了应用程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41545283/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check app is installed or not in phone
提问by torinpitchers
I don't know how to check if app is installed or not on phone! Or when App is installed, open the app, otherwise open the Appstore link to download the app. I'm using swift 3. I want to do it using app nameor bundle identifier. Thank You!
我不知道如何检查手机上是否安装了应用程序!或者安装App后打开App,否则打开Appstore链接下载App。我正在使用swift 3。我想使用app name或bundle identifier来做到这一点。谢谢你!
回答by torinpitchers
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")
}
}
回答by Jonathan Thornton
Between the other answers and their comments, I'm getting the impression that the asker wants to be able to see if any givenapp is installed.
在其他答案和他们的评论之间,我的印象是提问者希望能够查看是否安装了任何给定的应用程序。
Beginning with iOS 9.0, that is not possible.
从 iOS 9.0 开始,这是不可能的。
Apps for iOS 9 and later must have a list of requested URL schemes in the Info.plist before being allowed to use canOpenURL:
. This is to protect user privacy, as advertisers were abusing this functionality in an arguably invasive fashion. (See this excellent postfor more details on those changes.)
iOS 9 及更高版本的应用程序在被允许使用canOpenURL:
. 这是为了保护用户隐私,因为广告商正在以一种可以说是侵入性的方式滥用此功能。(有关这些更改的更多详细信息,请参阅这篇出色的帖子。)
Of course, that list is static and cannot be changed after build time or submission to the App Store. If Apple doesn't like the ones you chose, they have every right to reject it.
当然,该列表是静态的,在构建时间或提交到 App Store 后无法更改。如果 Apple 不喜欢你选择的那些,他们完全有权拒绝它。
I'm afraid that what you're asking isn't possible within reason for iOS 9.0 and later.
恐怕您要问的在 iOS 9.0 及更高版本的合理范围内是不可能的。
Edit: I also want to make clear that an app's URL scheme may not necessarily match nicely with its name. (This is more of an issue of a badly named constant than a functional issue.) There used to be a giant list of known URI schemes with documentation for each, but poignantly and fittingly enough, it seems that the wiki hosting it has shut down.
编辑:我还想说明一个应用程序的 URL 方案可能不一定与其名称很好地匹配。(这与其说是功能问题,不如说是一个命名错误的常量问题。)曾经有一个庞大的已知 URI 方案列表,每个方案都带有文档,但恰到好处地,托管它的 wiki 似乎已经关闭.
回答by Gurjinder Singh
Swift 4.1. One developer can have more than one app on AppStore. So, I need to check if user has installed other apps or not by the same developer. I had Bundle ID's of other apps. Although you can use Appnameinstead of Bundle Id. So I followed the following steps.
斯威夫特 4.1。一个开发者可以在 AppStore 上拥有多个应用程序。因此,我需要检查用户是否安装了同一开发人员的其他应用程序。我有其他应用程序的捆绑 ID。虽然您可以使用Appname而不是Bundle Id。所以我遵循了以下步骤。
In your current app add LSApplicationQueriesSchemes key of type Array in your info.plist file. Make entry of bundle id or Appname of app there which you want to open from your app.
Other app should have their bundle id or Appname entry in that app URL Scheme.
在您当前的应用程序中,在 info.plist 文件中添加 Array 类型的 LSApplicationQueriesSchemes 键。输入要从应用程序打开的应用程序包 ID 或应用程序名称。
其他应用程序应该在该应用程序 URL 方案中有他们的包 ID 或 Appname 条目。
- In your current app check if that app in installed in iPhone or not and can open accordingly.
- 在您当前的应用程序中检查该应用程序是否已安装在 iPhone 中并可以相应地打开。
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") }
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") }
- You can also test it from Safari browser. Just type the following in search bar
- 您也可以从 Safari 浏览器进行测试。只需在搜索栏中输入以下内容
URL_scheme:// or Bundle_Id://
URL_scheme:// or Bundle_Id://
If app is installed the it will show alert with Appname to open it in app.
如果安装了应用程序,它将显示带有 Appname 的警报以在应用程序中打开它。
回答by Arshad Shaik
After looking into so many answers, i am writing a common code which will help for new users. If you have two mobile apps as App1 and App2, if you want to check in App2 that App1 is already installed in his device or not, here is code below.
在查看了这么多答案之后,我正在编写一个通用代码,这将对新用户有所帮助。如果您有两个移动应用程序 App1 和 App2,如果您想在 App2 中检查 App1 是否已安装在他的设备中,这里是下面的代码。
In App1 add this property in info.plist file
<key>CFBundleURLTypes</key> <array> <dict> <key>CFBundleURLName</key> <string>com.companyName.App1</string> <key>CFBundleURLSchemes</key> <array> <string>App1</string> </array> </dict> </array>
In App2 add this property in info.plist file
<key>LSApplicationQueriesSchemes</key> <array> <string>App1</string> </array>
在 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>
In App2 write the method to check if app is installed or not on phone! Or when App is installed, open the app, otherwise open the Appstore link to download the app as below.
在 App2 中编写检查手机上是否安装了应用程序的方法!或者当安装了应用程序时,打开应用程序,否则打开应用程序商店链接下载应用程序,如下所示。
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)
}
}
}
}
I hope it will help some one.
我希望它会帮助某人。
回答by Imad Ali
This worked for me (Swift 3.0)
这对我有用(Swift 3.0)
Below two inputs should be provided:
应提供以下两个输入:
<APP URL SCEHME>
: The URL Scheme of the app which you want to open<YOUR APP URL>
: The App Itunes URLfunc 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) } }
<APP URL SCEHME>
:您要打开的应用程序的 URL Scheme<YOUR APP URL>
:应用程序 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) } }
回答by Mir Ashraf
first go to info.plist, add LSApplicationQueriesSchemes add an item and place instagram on that item. Now this code will run perfectly.
首先转到 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")
}