Xcode 8打开新的苹果地图

时间:2017-02-05 14:48:55

标签: ios swift nsurl

需要一些帮助。这是我的代码,当我点击按钮GoMap时,我被提示此错误 致命错误:在解包可选值时意外发现nil

@IBAction func GoMap(_ sender: UIButton) {   
 UIApplication.shared.openURL(NSURL(string: "http://maps.apple.com/maps?saddr=1 Republic Boulevard , Singapore 038975")! as URL)

}

1 个答案:

答案 0 :(得分:0)

您的网址无效。空格需要转义为%20:

URL(string: "http://maps.apple.com/maps?saddr=1%20Republic%20Boulevard,%20Singapore%20038975")!

或者,使用URLComponent + URLQueryItem让系统为您转义:

var components = URLComponents(string: "http://maps.apple.com/maps")!
components.queryItems = [URLQueryItem(name: "saddr", value: "1 Republic Boulevard, Singapore 038975")]
let url = components.url!
UIApplication.shared.openURL(url)