ios - 实例方法 'mapView(_:didFailToLocateUserWithError:)'几乎符合可选要求

标签 ios swift delegates mapkit

我在我的应用程序中使用 MKMapView 并且它工作正常。我可以通过 didUpdate userLocation 方法正确获取更新后的位置。

我的问题是这样的:

Instance method 'mapView(_:didFailToLocateUserWithError:)' nearly matches optional requirement 'mapView(_:didFailToLocateUserWithError:)' of protocol 'MKMapViewDelegate'

注释:

  • 我已经编写了如下方法:

    func mapView(_ mapView: MKMapView, didFailToLocateUserWithError error: Error) {
        //code
    }
    
  • 我已经实现了正确的协议(protocol):MKMapViewDelegate

  • didUpdateUserLocation 方法工作正常。

  • 我使用的是 Xcode 9.2,部署目标:10.0,Swift 3.2。

  • 我在方法签名处添加了@nonobjc,并且没有出现警告,但该方法从未执行。

最佳答案

确保已添加此内容

 mapView.showsUserLocation = true

并检查授权

let locationManager = CLLocationManager()
if CLLocationManager.authorizationStatus() == .notDetermined {
  locationManager.requestWhenInUseAuthorization()
} else if CLLocationManager.authorizationStatus() ==   .authorizedWhenInUse {
   mapView.showsUserLocation = true
} else {
   //show alert
}

关于ios - 实例方法 'mapView(_:didFailToLocateUserWithError:)'几乎符合可选要求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48323243/

相关文章:

iOS 11 - 成为第一响应者不打开键盘

delegates - HearthRateMonitor/BT101 Swift 示例 : discoverServices's delegate methods not called

ios - 委托(delegate)如何知道在有多个一致性的地方调用函数

ios - 将 MBProgressHUD 添加到 SDWebImage 和 UICollectionView

android - 室内 GPS 演示

ios - 如何使用正文和 URL 中的参数制作经过身份验证的 POST?

swift - Xcode 8/swift 3 : Simple UIPicker code not working

ios - 多屏支持 iOS 8+

ios - 选择弹出按钮清除 UI 文本字段

c# - 有没有办法直接使用 C# 方法作为委托(delegate)?