【问题标题】:-canOpenURL: failed for URL: "comgooglemaps://" - error: "The operation couldn’t be completed-canOpenURL:URL 失败:“comgooglemaps://” - 错误:“操作无法完成
【发布时间】:2018-07-10 10:04:33
【问题描述】:

我知道之前有人问过并回答过这个问题,但我仍然在调试器控制台中收到此错误。

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

环境:Xcode 9.4、Swift 3。
请告知我如何修复我的实施。希望其他人也会发现它有用。

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

【问题讨论】:

  • 你解决了吗?
  • 我做了,请看下面的答案
  • @NavNav 我的回答解决了你的问题吗?

标签: google-maps swift3


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:
      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!)
            }
        }
    }
    

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 2019-02-18
        • 2019-02-10
        • 1970-01-01
        • 2020-07-24
        • 1970-01-01
        • 2018-04-05
        • 1970-01-01
        • 2016-03-27
        • 2017-04-28
        相关资源
        最近更新 更多