How to Programmatically check and open an existing app in - Swift 4

给你一囗甜甜゛ 提交于 2020-01-07 09:45:15

问题


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.

I could opened the Appstore from the url,

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

by using the below code,

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?


回答1:


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.

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

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:

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

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.

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.




回答2:


Use Url schemes to open third party application.

var profile = "instagram://user?username=johndoe"
var instagramUrl = NSURL(string: profile)

if UIApplication.sharedApplication().canOpenURL(instagramUrl!) {  
    UIApplication.sharedApplication().openURL(instagramUrl!)

} else {
    //redirect to safari because the user doesn't have Instagram
    UIApplication.sharedApplication().openURL(NSURL(string: "http://instagram.com/")!)
}


来源:https://stackoverflow.com/questions/48072650/how-to-programmatically-check-and-open-an-existing-app-in-swift-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!