Swift: How to open a new app when UIButton is tapped

Try this. For example you want to open an Instagram app:

let instagramHooks = "instagram://user?username=johndoe"
let instagramUrl = URL(string: instagramHooks)!
if UIApplication.shared.canOpenURL(instagramUrl)
{  
    UIApplication.shared.open(instagramUrl)
} else {
    //redirect to safari because the user doesn't have Instagram
    UIApplication.shared.open(URL(string: "http://instagram.com/")!)
}

Leave a Comment