【问题标题】:How to redirect to Google map if installed, otherwise redirect to Appstore for install Google Map using Swift如果已安装,如何重定向到 Google 地图,否则如何重定向到 Appstore 以使用 Swift 安装 Google 地图
【发布时间】:2019-01-11 04:29:04
【问题描述】:

我需要在我的 iOS 应用中打开谷歌地图。如果安装了谷歌地图,导航到谷歌地图,否则重定向到 AppStore 以安装谷歌地图。这是我的尝试

 if (UIApplication.shared.canOpenURL(URL(string:"comgooglemaps://")!)) {

            print("app found")
            UIApplication.shared.open(URL(string: "comgooglemaps://?center=9.9894,76.5790&zoom=14&views=traffic&q=\(lattitude),\(longitude)")!, options: [:], completionHandler: nil)

        }else {

            showAlertWithTitle(title: "App not found", message: "You need to install Google Map", fromViewController: self)
            print("Can't use comgooglemaps://")
     }

【问题讨论】:

  • 请添加您的代码或尝试
  • @SagarChauhan 如果安装了,我可以导航到谷歌地图。如果未安装,需要导航 AppStore 以安装谷歌地图
  • @SPatel 添加了我的代码。请检查
  • 您是否在 info.plist 中添加了 LSApplicationQueriesSchemes ?看到这个developer.apple.com/documentation/uikit/uiapplication/…

标签: ios swift navigation


【解决方案1】:

一种选择是将UIApplication openURL 与以http://maps.google.com 开头的URL 一起使用。

如果用户安装了谷歌地图应用程序,该应用程序将被启动。

如果用户没有安装应用程序,Safari 将打开到谷歌地图页面。除了显示地图外,该页面还可以让用户在需要时选择前往 App Store 获取应用程序。

这种方法具有最大的灵活性,并为用户提供了他们想做的最大选择。

另一种方法是使用您所关注的方法并检查comgooglemaps 方案。在您的 else 中,您可以显示 SKStoreProductViewController,并传入 Google 地图应用 (585027354) 的应用 ID。

【讨论】:

  • 我喜欢这个主意。谢谢@rmaddy
【解决方案2】:
 if (UIApplication.shared.canOpenURL(URL(string:"comgooglemaps://")!)) {

            print("app found")
            UIApplication.shared.open(URL(string: "comgooglemaps://")!, options: [:], completionHandler: nil)

        }else {

            UIApplication.shared.openURL(NSURL(string: "itms://itunes.apple.com/us/app/google-maps-transit-food/id585027354?mt=8")! as URL)
            print("Can't use comgooglemaps://")
        }

另外请在您的 info.plist 中添加以下代码

<key>LSApplicationQueriesSchemes</key>
<array>
    <string>googlechromes</string>
    <string>comgooglemaps</string>
</array>

请添加代码并告诉我,上面的代码如果安装在您的设备中将打开谷歌地图,否则它将重定向到应用商店。

【讨论】:

    猜你喜欢
    • 2012-12-12
    • 2020-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多