尝试打开 google 映射 URL 时无法调用非函数类型 UIApplication 的值

Cannot call value of non-function type UIApplication when trying to open google maps URL

我是 swift 编程的新手,我偶然发现了一个问题,在网上搜索后似乎找不到解决方案。

我想做的是,当在我的应用程序中点击一个按钮时,我希望启动 Google 地图应用程序,但在实施代码后我收到错误消息:无法调用非函数值输入 UIApplication

我是不是漏掉了什么?

import UIKit
import GoogleMaps

class MapDisplayViewController: UIViewController, GMSMapViewDelegate, CLLocationManagerDelegate, UIApplicationDelegate {

@IBOutlet weak var openDirections: UIButton!

@IBAction func openMapsDirection(_ sender: UIButton!) {

    if (UIApplication.sharedApplication().canOpenURL(NSURL(string:"comgooglemaps://")!)) {
        UIApplication.sharedApplication().openURL(NSURL(string:
            "comgooglemaps://?center=40.765819,-73.975866&zoom=14&views=traffic")!)
    } else {
        print("Can't use comgooglemaps://");
    }
}

}

在 Swift 3 中 sharedApplication() 已替换为名为 shared 的 属性。

尝试更新您的代码:

if (UIApplication.shared.canOpenURL(NSURL(string:"comgooglemaps://")!)) {
    UIApplication.shared.openURL(NSURL(string:
        "comgooglemaps://?center=40.765819,-73.975866&zoom=14&views=traffic")!)
} else {
    print("Can't use comgooglemaps://");
}

"comgooglemaps:// and comgooglemaps-x-callback:// - These schemes allow you to launch the Google Maps app for iOS and perform one of several actions"

请确保您的设备已经安装了Google 地图应用。 https://developers.google.com/maps/documentation/ios-sdk/urlscheme

希望对您有所帮助。