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

查看:159
本文介绍了如何以编程方式检查并打开现有应用程序-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.

我可以从网址打开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方案,则无法启动(据我所知).

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

如果它确实实现了URL方案,则需要在您的应用中将该方案列入白名单.您可以通过将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方案的白名单.将其调整为您要打开的应用程序.

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天全站免登陆