I have an app and when a uibutton is clicked, I want to open another app that is already installed (i.e. Waze). How can I do such? Big thanks.
Asked
Active
Viewed 4.7k times
23
-
I want to open Waze that is already installed in my phone – thedansaps Nov 26 '15 at 06:57
-
1I think this is your answer. Hope it helps [http://stackoverflow.com/a/27697105/5417484](http://stackoverflow.com/a/27697105/5417484) – Vladimir Dimov Nov 26 '15 at 06:58
6 Answers
32
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/")!)
}

Michiel Dral
- 3,932
- 1
- 19
- 21

Orkhan Alizade
- 7,379
- 14
- 40
- 79
-
That code is changed to this: UIApplication.shared.openURL(NSURL(string: "http://instagram.com/")! as URL) – Mohammad Mirzakhani Jan 21 '19 at 18:32
-
Are there a way to how to do this for other apps? I can not seem to make it work with Facebook... – Chris Apr 27 '20 at 18:32
8
In SecondApp
Go to the plist file of SecondApp and you need to add a URL Schemes with a string iOSDevTips(of course you can write another string.it's up to you).
2 . In FirstApp
Create a button with the below action:
- (void)buttonPressed:(UIButton *)button
{
NSString *customURL = @"iOSDevTips://";
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:customURL]])
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:customURL]];
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"URL error"
message:[NSString stringWithFormat:@"No custom URL defined for %@", customURL]
delegate:self cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alert show];
}
}
That's it. Now when you can click the button in the FirstApp it should open the SecondApp.
For more info Refer here
4
You can look up Waze Community for reference.
Objective-C code snippet:
if ([[UIApplication sharedApplication]
canOpenURL:[NSURL URLWithString:@"waze://"]]) {
// Waze is installed. Launch Waze and start navigation
NSString *urlStr =
[NSString stringWithFormat:@"waze://?ll=%f,%f&navigate=yes",
latitude, longitude];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlStr]];
} else {
// Waze is not installed. Launch AppStore to install Waze app
[[UIApplication sharedApplication] openURL:[NSURL
URLWithString:@"http://itunes.apple.com/us/app/id323229106"]];
}
Swift code snippet:
if UIApplication.shared.canOpenURL(URL(string: "waze://")!) {
// Waze is installed. Launch Waze and start navigation
let urlStr = String(format: "waze://?ll=%f, %f&navigate=yes", latitude, longitude)
UIApplication.shared.openURL(URL(string: urlStr)!)
} else {
// Waze is not installed. Launch AppStore to install Waze app
UIApplication.shared.openURL(URL(string: "http://itunes.apple.com/us/app/id323229106")!)
}

wzso
- 3,482
- 5
- 27
- 48
-
4Note that when compiling with iOS SDK 9.0 and up, you need to update your application's plist with the following code to include Waze.
LSApplicationQueriesSchemes waze
3
In Swift 4 you can use:
if let url = URL(string: "\(myUrl)") {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}

Olcay Ertaş
- 5,987
- 8
- 76
- 112

Mohammad Mirzakhani
- 560
- 5
- 12
2
in swift4 waze
class FullMapVC: UIViewController {
var lat:CLLocationDegrees?
var long:CLLocationDegrees?
func wazeMaps()
{
let openUrl = URL(string: "waze://?ll=\(String(describing: lat!)),\(String(describing: long!))&navigate=yes")!
UIApplication.shared.open(openUrl , options:[:]) { (success) in
if !success
{
}
}
}
}
replace url with if you wanna use google maps
let openUrl = URL(string: "comgooglemaps://?saddr=&daddr=\(String(describing: lat!)),\(String(describing: long!))&directionsmode=driving")!

SpaceDust__
- 4,844
- 4
- 43
- 82
2
For Swift 4 / 5 / ..., you can do it properly like this :
if let url = URL(string: url), UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url)
} else {
// display error ?
}

Medhi
- 2,656
- 23
- 16