【问题标题】:Swift: How to open a new app when UIButton is tappedSwift:点击 UIButton 时如何打开一个新应用程序
【发布时间】:2016-02-29 04:12:33
【问题描述】:

我有一个应用程序,当单击 uibutton 时,我想打开另一个已安装的应用程序(即 Waze)。我怎么能这样做?非常感谢。

【问题讨论】:

标签: ios iphone xcode swift


【解决方案1】:

试试这个。例如你想打开一个 Instagram 应用:

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

【讨论】:

  • 代码改成这样:UIApplication.shared.openURL(NSURL(string: "instagram.com/")! as URL)
  • 有没有办法为其他应用程序执行此操作?我似乎无法让它与 Facebook 一起使用......
【解决方案2】:

在第二个应用程序中

进入SecondApp的plist文件,你需要添加一个带有字符串iOSDevTips的URL Schemes(当然你可以写另一个字符串,这取决于你)。

2 。在 FirstApp 中

使用以下操作创建一个按钮:

- (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];
  }

}

就是这样。现在,当您可以单击 FirstApp 中的按钮时,它应该会打开 SecondApp。

更多信息请参考here

【讨论】:

    【解决方案3】:

    您可以查找Waze Community 以供参考。

    Objective-C 代码 sn-p:

    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 代码 sn-p:

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

    【讨论】:

    • 请注意,使用 iOS SDK 9.0 及更高版本进行编译时,您需要使用以下代码更新应用程序的 plist 以包含 Waze。 LSApplicationQueriesSchemeswaze
    【解决方案4】:

    在 Swift 4 中,您可以使用:

    if let url = URL(string: "\(myUrl)") {
        UIApplication.shared.open(url, options: [:], completionHandler: nil)
    }
    

    【讨论】:

      【解决方案5】:

      在 swift4 位智中

      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
                  {
      
                  }
              }
          }
      
      }
      

      如果您想使用谷歌地图,请将网址替换为

       let openUrl = URL(string: "comgooglemaps://?saddr=&daddr=\(String(describing: lat!)),\(String(describing: long!))&directionsmode=driving")!
      

      【讨论】:

        【解决方案6】:

        对于 Swift 4 / 5 / ...,您可以像这样正确地做到这一点:

        if let url = URL(string: url), UIApplication.shared.canOpenURL(url) {
            UIApplication.shared.open(url)
        } else {
            // display error ?
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-05-28
          • 1970-01-01
          • 1970-01-01
          • 2012-10-22
          • 2015-07-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多