如何以编程方式检查和打开现有应用程序 - Swift 4 [英] How to Programmatically check and open an existing app in - Swift 4

查看:27
本文介绍了如何以编程方式检查和打开现有应用程序 - Swift 4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查我的手机中是否安装了应用程序.如果它已经安装,我想启动它.如果未安装,我需要打开 Appstore 链接.

I want to check whether an app is installed in my phone or not. If it is already installed I want to launch the same. If it not installed I need to open the Appstore link.

我可以从 url 打开 Appstore,

I could opened the Appstore from the url,

https://itunes.apple.com/us/app/stack-ar/id1269638287?mt=8

通过使用下面的代码,

let urlString = "https://itunes.apple.com/us/app/stack-ar/id1269638287?mt=8"
UIApplication.shared.open(URL(string: urlString)!, options: [:]) { (success) in

        }

如果安装了应用,我如何打开它?

How can I open the app if it is installed?

推荐答案

要从您的应用程序打开另一个应用程序,目标应用程序需要实现一个 URL 方案.一些应用程序记录其 URL 方案,而其他应用程序则没有.您可能需要询问目标应用程序的开发人员或检查 Info.plist 文件.

To open another App from your App the target App needs to implement a URL Scheme. Some Apps document their URL Schemes while others don't. You may need to ask the Developer of the Target App or check the Info.plist file.

如果应用没有实现 URL Scheme,则无法启动它(据我所知).

If an app does not implement a URL Scheme, there is no way of starting it (that I know of).

如果它确实实现了 URL Scheme,您需要在您的应用中将该方案列入白名单.您可以通过将 LSApplicationQueriesSchemes 添加到您的 Info.plist 来实现,如下所示:

If it does implement an URL Scheme, you need to whitelist that scheme in your app. You do that by adding LSApplicationQueriesSchemes to your Info.plist like shown here:

该图显示了与 Facebook 和 Twitter 对应的 URL Schemes 的白名单.针对您要打开的应用调整此设置.

The image shows the whitelisting of the URL Schemes corresponding with Facebook and Twitter. Adapt this for the app that you want to open.

完成后,您可以使用 UIApplication.sharedApplication().canOpenURL(...) 来检查应用程序是否已安装和 UIApplication.sharedApplication().openURL(...) 打开目标应用.

Once you have done that, you can use UIApplication.sharedApplication().canOpenURL(...) to check if the app is installed and UIApplication.sharedApplication().openURL(...) to open the target app.

我还想建议使用 SKStoreProductViewController 在 App Store 中显示应用程序,而不是打开 Safari.这样就可以直接在您的应用中呈现类似 App Store 的视图,而无需离开它.

I'd also like to suggest using SKStoreProductViewController to show the app in the App Store instead of opening Safari. This way it is possible to present an App Store like view directly in your App without leaving it.

这篇关于如何以编程方式检查和打开现有应用程序 - Swift 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆