Callout Accessory Tapped - 为什么网址打不开?一些简单的东西,我敢肯定

CalloutAccessoryTapped - Why won't a web address open? something simple im sure

所以我在玩 MKAnnotationView 我有权利 calloutaccessory.

因为我只有一个注释,所以我想我会尝试通过打开一个网页来使其尽可能简单 - google。但是,事实并非如此,我不明白为什么。我错过了什么?

func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {

    if control == view.rightCalloutAccessoryView {
        let url = NSURL(string: "google.com")
        UIApplication.shared.canOpenURL(url as! URL)
    }
}

它不是canOpenURL它用于检查应用程序是否能够打开url或

Returns a Boolean value indicating whether or not the URL’s scheme can be handled by some app installed on the device.

iOS 10 *UIApplication.shared.open(url, options: [:], completionHandler: nil) 否则你可以使用

UIApplication.shared.openURL(url)

根据文档

Important

If your app is linked on or after iOS 9.0, you must declare the URL schemes you want to pass to this method. Do this by adding the LSApplicationQueriesSchemes key to your app's Info.plist file. If you call this method for a scheme not declared using that key, this method always returns false, whether or not an appropriate app is installed. To learn more about the key, see LSApplicationQueriesSchemes.

使用完整的 url 打开 google 页面。试试这个:

let url = URL(string: "https://www.google.com")
UIApplication.shared.openURL(url!)