-canOpenURL:URL失败:“ comgooglemaps://”-错误:“操作无法完成

时间:2018-07-10 10:04:33

标签: google-maps swift3

我知道之前曾有人问过这个问题,但在调试器控制台中仍然出现此错误。

  

2018-07-10 12:55:15.173843 + 0300 brevcleaner [34011:2595874]   -canOpenURL:URL失败:“ comgooglemaps://”-错误:“操作无法完成。(OSStatus错误-10814。)”

环境:Xcode 9.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!)
        }
    }
}

3 个答案:

答案 0 :(得分:1)

您是否在plist中添加了comgooglemaps-x-callback?将其添加到LSApplicationQueriesSchemes下,

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

答案 1 :(得分:0)

  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!)
        }
    }
}

答案 2 :(得分:0)

是的,您需要如上所述添加到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)")!)
   }
}