Ich habe eine App und wenn ein Uibutton angeklickt wird, möchte ich eine andere App öffnen, die bereits installiert ist (z. B. Waze). Wie kann ich das machen? Vielen Dank.
Versuche dies. Zum Beispiel möchten Sie eine Instagram-App öffnen:
var instagramHooks = "instagram://user?username=johndoe"
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
UIApplication.sharedApplication().openURL(NSURL(string: "http://instagram.com/")!)
}
In SecondApp
Gehen Sie zur plist-Datei von SecondApp und Sie müssen ein URL-Schema mit einer Zeichenfolge iOSDevTips hinzufügen (natürlich können Sie eine weitere Zeichenfolge schreiben. Es liegt an Ihnen).
2 In der FirstApp
Erstellen Sie eine Schaltfläche mit der folgenden Aktion:
- (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];
}
}
Das ist es. Wenn Sie jetzt auf die Schaltfläche in der FirstApp klicken können, sollte die SecondApp geöffnet werden.
Für weitere Informationen siehe hier
Sie können Waze Community als Referenz suchen.
Code-Snippet für Ziel-C:
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"]];
}
Schnelles 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")!)
}
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
{
}
}
}
}
ersetzen Sie die URL mit, wenn Sie Google Maps verwenden möchten
let openUrl = URL(string: "comgooglemaps://?saddr=&daddr=\(String(describing: lat!)),\(String(describing: long!))&directionsmode=driving")!
in Swift 4:
if let url = URL(string: "\(myUrl)") {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}