【问题标题】:Moovit Integration Causes CrashMoovit 集成导致崩溃
【发布时间】:2017-07-10 10:40:47
【问题描述】:

我正在尝试在我的应用中实施 Moovit,以便用户可以轻松获得前往某个地点的公交路线。

但是我遇到了一些困难......

更新代码:

func openMoovit(To : CLLocationCoordinate2D) {
    if UIApplication.shared.canOpenURL(URL(string: "moovit://")!) {
        // Moovit installed - launch app (with parameters)
        let MoovitURL: String = "moovit://directions?dest_lat=\(To.latitude)&dest_lon=\(To.longitude)&dest_name=\(barNameTemplate))&auto_run=true&partner_id=<TestApp>"
        let escapedString = MoovitURL.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)
        UIApplication.shared.openURL(URL(string: escapedString!)!)
        }else {
// Moovit not installed - send to store
UIApplication.shared.openURL(URL(string: "https://itunes.apple.com/us/app/id498477945")!)

这是Moovit iOS API的基础

然后在我按下按钮时简单地调用该函数:

let MoovitButton = UIAlertAction(title: "Moovit", style: .default) { action -> Void in
                
self.openMoovit(To : self.CoordinatesTemplate)// calling function
print("Moovit Chosen!")

此代码在 Waze 集成中运行良好,但在 Moovit 中失败... 当我按下按钮时它会崩溃在线

UIApplication.shared.openURL(URL(string: MoovitURL)!)

说:

fatal error: unexpectedly found nil while unwrapping an Optional value
(lldb)

我也将 moovit 添加到了我的 Plist 中,所以我不知道是什么导致了崩溃...我错过了什么吗?

如果有人能帮我解决这个问题,我将不胜感激,谢谢。

【问题讨论】:

  • URL(string: MoovitURL)! 无法生成 URL 对象,因此您可以通过 printing print(URL(string: MoovitURL)) 进行验证
  • 具体你需要对你的URL进行urlencode;它包含一个需要转换为 %20 的空格

标签: ios swift url crash fatal-error


【解决方案1】:

它会起作用的,

由于您的字符串Times Square 中有空格,无法生成 URL 对象:

func openMoovit(To : CLLocationCoordinate2D) {
    if UIApplication.shared.canOpenURL(URL(string: "moovit://")!) {
         // Moovit installed - launch app (with parameters)
         let MoovitURL: String = "moovit://directions?dest_lat=40.758896&dest_lon=-73.985130&dest_name=Times Square&orig_lat=40.735845&orig_lon=-73.990512&orig_name=Union Square&auto_run=true&partner_id=<TestApp>"
         var escapedString = MoovitURL.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)
         UIApplication.shared.openURL(URL(string: escapedString)!)
    }

【讨论】:

  • 好的,它现在打开了 Moovit,但由于某种原因它没有开始方向......查看问题上方的更新代码,你已经解决了我的问题,它没有打开 Moovit,但如果它不要问太多,你能看看这个吗?谢谢。
  • 好的,我在找
  • (To.latitude)&amp;dest_lon=\(To.longitude) 检查这个我认为它可能是一个可选值,所以你必须解开它......
  • 即使 Moovit 给出的示例本身也不起作用:"moovit://directions?dest_lat=40.758896&amp;dest_lon=-73.985130&amp;dest_name=Times Square&amp;orig_lat=40.735845&amp;orig_lon=-73.990512&amp;orig_name=Union Square&amp;auto_run=true&amp;partner_id=&lt;TestApp&gt;"
【解决方案2】:

将 urlString 转换为 URL 将返回 nil 并且您正在强制解开 nil。因此它崩溃了。

您应该在打开包装之前检查nil

if UIApplication.shared.canOpenURL(URL(string: "moovit://")!) {
       // Moovit installed - launch app (with parameters)
       let MoovitURL: String = "moovit://directions?dest_lat=40.758896&dest_lon=-73.985130&dest_name=Times Square&orig_lat=40.735845&orig_lon=-73.990512&orig_name=Union Square&auto_run=true&partner_id=<TestApp>"
       if let url = URL(string: MoovitURL) {
            UIApplication.shared.openURL(url)
       }
}

别忘了在您的网址中添加您的合作伙伴 ID。

【讨论】:

    猜你喜欢
    • 2019-04-15
    • 2013-08-24
    • 2020-11-19
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    • 2018-01-23
    • 2020-12-02
    • 2013-11-11
    相关资源
    最近更新 更多