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

后端 未结 5 543
一生所求
一生所求 2020-11-29 10:18

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.

5条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-29 11:07

    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")!)
    }
    

提交回复
热议问题