• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

使用MapKit和CoreLocation时的Xcode警告

用户头像
it1352
帮助1

问题说明

我尝试使用 MKMapView 的实例,使用 CoreLocation 跟踪用户位置,然后放大到它们所在的位置。

I'm attempting to use implement an instance of MKMapView, use CoreLocation to track the users location, and then zoom in to where they are.

我只想在前台处追踪使用者的位置。由于我的应用程序针对iOS8,我有一个plist条目的关键 NSLocationWhenInUseUsageDescription

I only want to track the user's location when I'm in the foreground. Since my app is targeted for iOS8, I have a plist entry for the key NSLocationWhenInUseUsageDescription.

当我第一次运行应用程序时,应用程序会相应地询问是否可以访问我的位置。点击允许后,我会从Xcode收到以下警告:

When I run the app for the first time, the app appropriately asks if it can access my location. After I click 'Allow', I then receive the following warning from Xcode:

尝试在不提示进行位置授权的情况下启动MapKit位置更新。必须调用 - [CLLocationManager requestWhenInUseAuthorization]或 - [CLLocationManager requestAlwaysAuthorization]。

...这有点混乱,因为我在事实调用 requestWhenInUseAuthorization ,如下面的代码所示:

...which is a bit confusing, as I am in fact calling requestWhenInUseAuthorization, as can be seen in my code below:

@property (strong, nonatomic) IBOutlet MKMapView *mapView;
@property(nonatomic, retain) CLLocationManager *locationManager;

@end

@implementation MapView

- (void)viewDidLoad {
    [super viewDidLoad];
    [self locationManager];
    [self updateLocation];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    self.locationManager = nil;
}

- (CLLocationManager *)locationManager {
    //We only want to get the location when the app is in the foreground
    [_locationManager requestWhenInUseAuthorization];
    if (!_locationManager) {
        _locationManager = [[CLLocationManager alloc] init];
        _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    }
    return _locationManager;
}

- (void)updateLocation {
    _mapView.userTrackingMode = YES;
    [self.locationManager startUpdatingLocation];
}

有没有人了解为什么会发生此警告?

Does anyone have any insight into why this warning would be occurring?

正确答案

#1

您正在呼叫 requestWhenInUseAuthorization 但您要等到获得授权吗?不,你不是。您(作为用户)点击允许,但是发生的时间太晚了:您的代码已经已经续,直接告诉地图视图开始跟踪用户的位置。

You are calling requestWhenInUseAuthorization, that is true. But are you waiting until you get authorization? No, you are not. You (as the user) are tapping Allow, but that's happening too late: your code has already continued, going straight on to tell the map view to start tracking the user's location.

只需查看 requestWhenInUseAuthorization 上的文档:


当前授权状态为kCLAuthorizationStatusNotDetermined时,此方法异步运行

When the current authorization status is kCLAuthorizationStatusNotDetermined, this method runs asynchronously

异步运行

并且文档继续说:

状态确定后,位置管理器将结果传递给委托的 locationManager:didChangeAuthorizationStatus:方法

After the status is determined, the location manager delivers the results to the delegate’s locationManager:didChangeAuthorizationStatus: method

因此,实现该方法。如果您刚刚获得了权限, 是您可以开始使用位置管理器的信号。

So, implement that method. If you have just obtained permission, that is the signal that you can start using the location manager.

此外,您缺少一个重要步骤:你不是 是什么状态。如果状态不确定,您只应请求授权。如果状态受到限制或拒绝,您不能使用位置管理器;

Also, you are missing an important step: you are not checking what the status actually is. You should only be asking for authorization if the status is undetermined. If the status is restricted or denied, you must not use the location manager at all; and if the status is granted, there is no point asking for authorization again.

因此,总而言之,您的逻辑流程图应该是:

So, just to sum up, your logical flowchart should be:

  • 检查状态。

  • Check status.

状态是否为限制?停止。您不能在地图上使用获取位置更新或执行位置操作。

Is the status Restricted or Denied? Stop. You cannot use get location updates or do location on a map.

授予的状态是否正确?继续在地图上获取位置更新或执行位置。

Is the status Granted? Proceed to get location updates or do location on a map.

状态未确定?请求授权和停止。将 locationManager:didChangeAuthorizationStatus:作为授权请求的完成处理程序。此时,回到流程图的开始!

Is the status Undetermined? Request authorization and stop. Treat locationManager:didChangeAuthorizationStatus: as the completion handler for your authorization request. At that point, go back to start of the flowchart!

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanhccigkg
系列文章
更多 icon
同类精品
更多 icon
继续加载