【发布时间】: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