简体   繁体   English

Moovit集成导致崩溃

[英]Moovit Integration Causes Crash

Im trying to implement Moovit into my app so users can easily get transit directions to a place. 我正在尝试将Moovit实施到我的应用中,以便用户可以轻松获取到某个地方的公交路线。

However I'm encountering some difficulties... 但是我遇到了一些困难...

Updated Code: 更新的代码:

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

which is the basic from the Moovit iOS API 这是Moovit iOS API的基础

and then simply called the function when i press a button : 然后当我按下一个按钮时简单地调用该函数:

let MoovitButton = UIAlertAction(title: "Moovit", style: .default) { action -> Void in

self.openMoovit(To : self.CoordinatesTemplate)// calling function
print("Moovit Chosen!")

This code is working well with Waze integration but fails with Moovit... When i press the button it crashes at line : 这段代码在Waze集成中运行良好,但是在Moovit中运行失败...当我按下按钮时,它在第1行崩溃:

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

saying : 说:

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

I'v added moovit into my Plist as well , so i don't know what is causing the crash...Did i miss anything? 我也将moovit添加到了我的Plist中,所以我不知道是什么原因导致了崩溃...我错过了什么吗?

i would really appreciate if anyone could help me solve this, Thanks ahead. 如果有人可以帮助我解决这个问题,我将不胜感激,谢谢。

Converting urlString to URL is returning nil and you are forcefully unwrapping nil . 将urlString转换为URL会返回nil并且您正在强制展开nil Due to that it's crashing. 因此,它崩溃了。

You should check nil before unwrapping 您应该在展开前检查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)
       }
}

And Don't forgot to add your parter id in your url. 并且不要忘记在您的URL中添加您的伙伴ID。

It will work, 会工作的,

Due to space in your string Times Square , it is unable to make URL object: 由于您的字符串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)!)
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM