【发布时间】:2015-12-18 11:34:30
【问题描述】:
我使用的是 swift 2.0、XCode 7.2,部署目标是 9.2。 单击按钮时,我希望重定向到苹果地图以显示位置。以下是我的代码不起作用:
var addr = self.sortedDict.valueForKey("ADDRESS1").objectAtIndex(cellIndexPath!.row).objectAtIndex(0) as! String + "," + (self.sortedDict.valueForKey("CITY").objectAtIndex(cellIndexPath!.row).objectAtIndex(0) as! String) + "," + (self.sortedDict.valueForKey("STATE").objectAtIndex(cellIndexPath!.row).objectAtIndex(0) as! String)
let strUrl: String = "http://maps.apple.com?address=" + addr
let url: NSURL = NSURL(string: strUrl.stringByAddingPercentEncodingWithAllowedCharacters(.URLHostAllowedCharacterSet())!)!
if UIApplication.sharedApplication().canOpenURL(url) {
UIApplication.sharedApplication().openURL(url)
} else {
print(UIApplication.sharedApplication().canOpenURL(url))
}
我得到的错误是:
-canOpenURL: failed for URL: "http%3A%2F%2Fmaps.apple.com%3Faddress=4702%20Katy%20Hockley%20Cut%20Off%20Rd,Katy,TX" - error: "Invalid input URL"
false
在 iOS 9 中,您必须将您的应用想要在 Info.plist 中的 LSApplicationQueriesSchemes 键下查询的任何 URL 方案列入白名单。
我应该为苹果地图指定什么键?
【问题讨论】:
-
Apple 地图无法删除,何必检查
canOpen- 只需打开网址 -
你的网址基本上是无效的,让它有效,然后它会工作。提示:不要对 URL 中根本不能编码的部分进行百分比编码。