-canOpenURL: failed for URL: "comgooglemaps://" - error: "The operation couldn’t be completed

-canOpenURL: failed for URL: "comgooglemaps://" - error: "The operation couldn’t be completed

我知道这个问题之前已经被问过和回答过,但我仍然在调试器控制台中收到这个错误。

2018-07-10 12:55:15.173843+0300 brevcleaner[34011:2595874] -canOpenURL: failed for URL: "comgooglemaps://" - error: "The operation couldn’t be completed. (OSStatus error -10814.)"

环境:Xcode9.4,Swift3.
请告知我如何修复我的实现。希望其他人也会发现它有用。

Info.plist 文件

 <key>NSLocationWhenInUseUsageDescription</key>
<string>Will you allow myApp to know your location?</string>
<key>LSApplicationQueriesSchemes</key>
<array>
    <string>tel</string>
    <string>googlechromes</string>
    <string>comgooglemaps</string>
</array>

和代码;

func openMapsApp(destinationLatitude:String, destinationLongitude:String, cleanerAddressNow:String) {

    self.locationManager.stopUpdatingLocation()

    guard let _ = self.cleanerCurrentLatitude else  {
        print("cleanerCurrentLatitude not received line \(#line)")
        return
    }

    guard let _ = self.cleanerCurrentLongitude else {
        print("cleanerCurrentLongitude not received line \(#line)")
        return
    }


    //For Apple Maps
    let testURL2 = URL.init(string: "http://maps.apple.com/")
    //For Google Maps
    let testURL = URL.init(string: "comgooglemaps://")

    guard let _ = testURL2 else  {return}
    guard let _ = testURL else  {return}

    //For Google Maps
    if UIApplication.shared.canOpenURL(testURL!) {


        let sourceAddress = cleanerAddressNow.replacingOccurrences(of: " ", with: "+")
        let direction = String(format: "comgooglemaps://?daddr=%@&dirflg=%@&x-success=sourceapp://?resume=true&x-source=AirApp", sourceAddress,"r")

        let directionsURL = URL.init(string: direction)
        if #available(iOS 10, *) {
            UIApplication.shared.open(directionsURL!)
        } else {
            UIApplication.shared.openURL(directionsURL!)
        }
    }
        //For Apple Maps
    else if UIApplication.shared.canOpenURL(testURL2!) {

        let sourceAddress = cleanerAddressNow.replacingOccurrences(of: " ", with: "+")
        let direction = String(format: "http://maps.apple.com/?daddr=%@&dirflg=%@", sourceAddress,"r")

        let directionsURL = URL.init(string: direction)
        if #available(iOS 10, *) {
            UIApplication.shared.open(directionsURL!)
        } else {
            UIApplication.shared.openURL(directionsURL!)
        }

    }
        //For SAFARI Browser
    else {
        let sourceAddress = cleanerAddressNow.replacingOccurrences(of: " ", with: "+")
        let direction = String(format: "http://maps.apple.com/?daddr=%@&dirflg=%@", sourceAddress,"r")

        let directionsURL = URL.init(string: direction)
        if #available(iOS 10, *) {
            UIApplication.shared.open(directionsURL!)
        } else {
            UIApplication.shared.openURL(directionsURL!)
        }
    }
}
  func prepareToOpenMaps(fullAddress: String) {

    self.locationManager.startUpdatingLocation()

    let addressFormatted = fullAddress.replacingOccurrences(of: ",", with: " ")
    self.convenience.getLocaltionFrom(address: addressFormatted) { (latitude , longitude) in

        guard let lat = latitude else {return}
        guard let lon = longitude else {return}

        self.openMapsApp(destinationLatitude: lat, destinationLongitude: lon, cleanerAddressNow: addressFormatted)
    }
}


func openMapsApp(destinationLatitude:String, destinationLongitude:String, cleanerAddressNow:String) {

     self.locationManager.stopUpdatingLocation()

    guard let _ = self.cleanerCurrentLatitude else  {
        print("cleanerCurrentLatitude not received line \(#line)")
        return
    }

    guard let _ = self.cleanerCurrentLongitude else {
        print("cleanerCurrentLongitude not received line \(#line)")
        return
    }



    //For Google Maps
    let testURL = URL.init(string: "comgooglemaps-x-callback://")

    //For Apple Maps
    let testURL2 = URL.init(string: "http://maps.apple.com/")

    guard let _ = testURL2 else  {return}
    guard let _ = testURL else  {return}

    //For Google Maps
    if UIApplication.shared.canOpenURL(testURL!) {

        print("it can open google maps ")

        let sourceAddress = cleanerAddressNow.replacingOccurrences(of: " ", with: "+")
        let direction = String(format: "comgooglemaps-x-callback://?daddr=%@&dirflg=%@&x-success=sourceapp://?resume=true&x-source=AirApp", sourceAddress,"r")

        let directionsURL = URL.init(string: direction)
        if #available(iOS 10, *) {
            UIApplication.shared.open(directionsURL!)
        } else {
            UIApplication.shared.openURL(directionsURL!)
        }
    }
        //For Apple Maps
    else if UIApplication.shared.canOpenURL(testURL2!) {

        print("can open Apple maps")



        let sourceAddress = cleanerAddressNow.replacingOccurrences(of: " ", with: "+")
        let direction = String(format: "http://maps.apple.com/?daddr=%@&dirflg=%@", sourceAddress,"r")

        let directionsURL = URL.init(string: direction)
        if #available(iOS 10, *) {
            UIApplication.shared.open(directionsURL!)
        } else {
            UIApplication.shared.openURL(directionsURL!)
        }

    }
        //For SAFARI Browser
    else {

        print("can open safari")
        let sourceAddress = cleanerAddressNow.replacingOccurrences(of: " ", with: "+")
        let direction = String(format: "http://maps.apple.com/?daddr=%@&dirflg=%@", sourceAddress,"r")

        let directionsURL = URL.init(string: direction)
        if #available(iOS 10, *) {
            UIApplication.shared.open(directionsURL!)
        } else {

            print("opens directionsURL")
            UIApplication.shared.openURL(directionsURL!)
        }
    }
}

你在 plist 中添加了 comgooglemaps-x-callback 了吗??在LSApplicationQueriesSchemes

下添加
<key>LSApplicationQueriesSchemes</key>
    <array>
        <string>comgooglemapsurl</string>
        <string>comgooglemaps</string>
        <string>comgooglemaps-x-callback</string>
    </array>

是的,您需要如上所述添加到 LSApplicationQueriesString。那么你只需要以下几行:

var address = "..."
address = address.replacingOccurrences(of: " ", with: "+")
UIApplication.shared.open(URL(string:"comgooglemaps://?q=\(address)&views=traffic")!, options: [:]) { success in
   if !success {
       UIApplication.shared.open(URL(string: "http://maps.apple.com/?address=\(address)")!)
   }
}