iOS 9 从应用程序重定向到苹果 map

标签 ios swift ios9

我使用的是 swift 2.0、XCode 7.2,部署目标是 9.2。 单击按钮后,我希望重定向到苹果 map 以显示位置。以下是我的代码,但不起作用:

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 方案列入白名单。

我应该为苹果 map 指定什么键?

最佳答案

您可以使用MKMapItem直接打开苹果 map ,而不用打开URL

//your address to be opened
let address: String = "4702 Katy Hockley Cut Off Rd,Katy,TX"
let geocoder: CLGeocoder = CLGeocoder()
// Get place marks for the address to be opened
geocoder.geocodeAddressString(address, completionHandler: {(placemarks: [CLPlacemark]?, error: NSError?) -> Void in
    if (placemarks?.count > 0) {
        if let placemark = placemarks?.first {
            let mkPlacemark  = MKPlacemark(placemark: placemark)

            //Create map item and open in map app 
            let item = MKMapItem(placemark: mkPlacemark)
            item.name = address
            item.openInMapsWithLaunchOptions(nil)
        }

    }
})

关于iOS 9 从应用程序重定向到苹果 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34354750/

相关文章:

ios - AudioKit - 立体声 channel 从输入翻转到输出?

ios - 为表格 View 的高度分配上限?

ios - 未通过 TestFlight 收到推送通知

ios - 如何使用 Objective C 增加 UItableView 中的 UIView 高度?

ios - 使用 Swift 4.2 解析嵌套 JSON

ios - 在 Swift 4 中从计算变量创建关键路径

ios - 尝试启动 MapKit 位置更新而不提示在 ios 9 中使用 objective-c 进行位置授权

ios - Alamofire 的 Swift 扩展方法返回 SwiftyJSON 结果

swift - 自定义 UITabBarItem 的文本颜色和字体导致 swift 出现奇怪的结果

php - 从 iOS 9/Swift 2.0 发送 HTTP POST 请求时,PHP 服务器未收到正确的数据