如果已安装,如何重定向到谷歌地图,否则重定向到亚马逊应用商店以使用 Swift 安装谷歌地图



我需要在我的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://")
     }

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

如果用户安装了 Google 地图应用,系统就会启动该应用。

如果用户没有安装该应用程序,Safari 浏览器将打开到 Google 地图页面。除了显示地图外,如果需要,该页面还将为用户提供前往App Store以获取应用程序的选项。

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

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

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

另外,请在您的信息列表中添加以下代码

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

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

最新更新