I want to check whether an app is present in my iphone or not. If it is present I want to launch it else open the App Store link of the app.
Please suggest the required parameters to do so and way to implement it.
I have the App Store link to open the app but I need to check if app is already present in the phone.
You can check if the respective device has an app is installed using:
// url = "example" (check the following image to understand)
let canOpen = UIApplication.sharedApplication().canOpenURL(NSURL(string: url as String)!)
Where url
is pre configured (have a URL scheme registered for your app in your Info.plist)
So, if canOpen is true
means that app is installed and you can open using:
UIApplication.sharedApplication().openURL(NSURL(string:url)!)
try this code
swift 2.2
@IBAction func openInstaApp(sender: AnyObject) {
var instagramHooks = "instagram://user?username=your_username"
var instagramUrl = NSURL(string: instagramHooks)
if UIApplication.sharedApplication().canOpenURL(instagramUrl!)
{
UIApplication.sharedApplication().openURL(instagramUrl!)
} else {
//redirect to safari because the user doesn't have Instagram
println("App not installed")
UIApplication.sharedApplication().openURL(NSURL(string: "https://itunes.apple.com/in/app/instagram/id389801252?m")!)
}
}
swift 3
@IBAction func openInstaApp(sender: AnyObject) {
let instagramHooks = "instagram://user?username=your_username"
let instagramUrl = URL(string: instagramHooks)
if UIApplication.shared.canOpenURL(instagramUrl! as URL)
{
UIApplication.shared.open(instagramUrl!)
} else {
//redirect to safari because the user doesn't have Instagram
print("App not installed")
UIApplication.shared.open(URL(string: "https://itunes.apple.com/in/app/instagram/id389801252?m")!)
}
}
For opening an app from inside another app, you need to expose a URL scheme from the app you want to open.
If the app can be opened using openURL
function (which uses the custom URL scheme exposed), user can directly access your app.
If not, you can open app store app page using the openURL
function, supplying itunes app URL.
Here lies complete idea about how to achieve it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With