首页 > 解决方案 > iOS 上的 Uber 深度链接取货位置问题

问题描述

我在我的应用程序中使用 Uber 和 Lyft 深度链接。其功能是当用户点击“乘坐优步”或“乘坐 lyft”按钮时,它将打开 Uber 或 Lyft 应用程序,并将上车和目的地坐标发送给 Uber/Lyft,由 Uber/Lyft 应用程序处理行程。

我为 Uber 使用了标准的深度链接。当 Uber 已经打开并在后台运行时,一切正常。当 Uber 不在后台时,上车地点变成用户当前位置(这是错误的,因为用户要指定上车地点,不一定是当前位置)。不过,这个问题在 Lyft 中不会发生。

这是我的代码:

class ViewController: UIViewController {

    @IBOutlet weak var uberBtn: UIButton!
    @IBOutlet weak var lyftBtn: UIButton!

    @IBAction func uberClicked(_ sender: Any) {
        if isInstalledOf(app: "uber") {
            open(scheme: "uber://?action=setPickup&client_id=7r6zjXf5e1p4nqYkX17d9R44estKs-na&product_id=a12ab23b-66f0-4028-9bb9-856dbcfdbbc7&pickup[formatted_address]=5874%20Newberry%20Street%2C%20Romulus%2C%20MI%2C%20USA&pickup[latitude]=42.265570&pickup[longitude]=-83.387391&dropoff[formatted_address]=15609%20Regina%20Avenue%2C%20Allen%20Park%2C%20MI%2C%20USA&dropoff[latitude]=42.253737&dropoff[longitude]=-83.211945")
        } else {
            open(scheme: "https://m.uber.com/ul/")
        }
    }

    @IBAction func lyftClicked(_ sender: Any) {
        if isInstalledOf(app: "lyft") {
            open(scheme: "lyft://ridetype?id=lyft&pickup[latitude]=42.265570&pickup[longitude]=-83.387391&destination[latitude]=42.253737&destination[longitude]=-83.211945")
        } else {
            open(scheme: "https://www.lyft.com/signup/SDKSIGNUP?clientId=IcEuyAmFO7Gp&sdkName=iOS_direct")
        }
    }

    // check if the app is installed
    func isInstalledOf(app: String) -> Bool {
        return UIApplication.shared.canOpenURL(URL(string: "\(app)://")!)
    }

    func open(scheme: String) {
        if let url = URL(string: scheme) {
            if #available(iOS 10, *) {
                UIApplication.shared.open(url, options: [:], completionHandler: nil)
            } else {
                UIApplication.shared.openURL(url)
            }
        }
    }
}

标签: iosswiftuber-apideeplink

解决方案


使用 Uber SDK 并传递深度链接所需的参数。要解决取件地点问题,您需要传递取件名称和取件地址

        let pickupLocation          = CLLocation(latitude: <pickup latitude>, longitude: <pickup longitude>)
        let dropoffLocation         = CLLocation(latitude: <destination latitude>, longitude: <destination longitude>)
        let builder                 = RideParametersBuilder()
        builder.pickupLocation      = pickupLocation
        builder.pickupNickname      = <pickup nickname>
        builder.pickupAddress       = <pickup address>
        builder.dropoffLocation     = dropoffLocation
        builder.dropoffNickname     = <dropof nickname>
        builder.dropoffAddress      = <dropof address>
        builder.productID           = productId
        let deeplink                = RequestDeeplink(rideParameters: builder.build(), fallbackType: .appStore)
        deeplink.execute()

推荐阅读