Home > Net >  In swift, How can I redirect to any app by clicking button from my application? [closed]
In swift, How can I redirect to any app by clicking button from my application? [closed]

Time:09-29

in iOS swift - I have a list of apps and I have bundle Id, app URL, app name. Is it possible to redirect to the app which is installed in my device? I have list of all apps URL in appstore but not able to redirect to particular app. Like if I click on facebook button from my app, it must be redirect to facebook app if installed in my device. I can't add URL scheme for all apps coz its large number.

If any idea about it please let me know. Thanks.

CodePudding user response:

Use below function.

func openApp(appId : String) {
    if let url = URL(string: "itms-apps://itunes.apple.com/app/\(appId)"),
        UIApplication.shared.canOpenURL(url)
    {
        if #available(iOS 10.0, *) {
            UIApplication.shared.open(url, options: [:], completionHandler: nil)
        } else {
            UIApplication.shared.openURL(url)
        }
    }
}

Usage

openApp(appId: "id284882215") 

This will open facebook app else take to app store.

CodePudding user response:

we can use like this. For example you want to open an Instagram application

let instagram = "instagram://user?username=johndoe"
let instagramUrl = URL(string: instagram)!
if UIApplication.shared.canOpenURL(instagramUrl)
{  
    UIApplication.shared.open(instagramUrl)
} else {
    UIApplication.shared.open(URL(string: "http://instagram.com/")!)
}
  • Related