简体   繁体   中英

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

I'm new to swift programming and I stumbled upon a problem which can't seem to find a solution after searching the web.

What i'm trying to do is when a button is tapped in my application I want the Google maps app to launch but after implementing the code I get the error: Cannot call value of non-function type UIApplication

Am I missing something?

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

}

In Swift 3 sharedApplication() has been replaced with a property called shared .

Try updating your code:

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"

Please make sure that your device already installed Google Maps app. https://developers.google.com/maps/documentation/ios-sdk/urlscheme

Hope this will help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM