requestAlwaysAuthorization

requestAlwaysAuthorization

本文介绍了 - [CLLocationManager requestWhenInUseAuthorization]或 - [CLLocationManager requestAlwaysAuthorization]错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码,显示地图上当前位置的提醒和蓝点:

This is my code, showing both the alert and the blue dot for the current position on the map:

MapName.h

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>


@interface MapName : UIViewController <MKMapViewDelegate, CLLocationManagerDelegate>

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

@end

MapName.m

- (void)viewDidLoad
{
[super viewDidLoad];

self.locationManager = [[CLLocationManager alloc]init];
self.locationManager.delegate = self;
if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
    [self.locationManager requestWhenInUseAuthorization];
}
[self.locationManager startUpdatingLocation];

//Center the map
[self gotoLocation];

//Show current position
_MapName.showsUserLocation = YES;

}

我添加了密钥 NSLocationWhenIsUseUsageDescription 作为Info.plist的字符串。
我在Xcode上仍然遇到同样的错误。

I've added the key NSLocationWhenIsUseUsageDescription as a string to the Info.plist.I'm still getting the same error on Xcode.

推荐答案

这是由于两者:

[self.locationManager startUpdatingLocation];

_MapName.showsUserLocation = YES;

在打电话之前,您需要检查用户是否已获得许可。还要确保你在故事板上的MKMapKit中关闭用户位置(这个花了我几天的时间来追踪)。

You need to check if the user has given permission before you call these. Also make sure you turn off User Location in the MKMapKit on the storyboard (this one took me days to track down).

做类似的事情:

CLAuthorizationStatus authorizationStatus= [CLLocationManager authorizationStatus];

if (authorizationStatus == kCLAuthorizationStatusAuthorized ||
    authorizationStatus == kCLAuthorizationStatusAuthorizedAlways ||
    authorizationStatus == kCLAuthorizationStatusAuthorizedWhenInUse) {

    [self.locationManager startUpdatingLocation];
    _MapName.showsUserLocation = YES;

}

根据您的应用程序,您可能不想要求用户在启动时的许可,因为不推荐。

Depending on your app you may not want to ask for the user's permission on launch since that is not recommended.

这篇关于 - [CLLocationManager requestWhenInUseAuthorization]或 - [CLLocationManager requestAlwaysAuthorization]错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 19:13