- [CLLocationManager requestWhenInUseAuthorization]或 - [CLLocationManager requestAlwaysAuthorization]错误

时间:2014-09-20 15:32:11

标签: iphone mapkit ios8 core-location xcode6

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

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上仍然遇到同样的错误。

6 个答案:

答案 0 :(得分:15)

这是由于两者:

[self.locationManager startUpdatingLocation];

_MapName.showsUserLocation = YES;

在调用这些用户之前,您需要检查用户是否已授予权限。另外,请确保在故事板上关闭MKMapKit中的用户位置(这个花了我几天的时间来追踪)。

做类似的事情:

CLAuthorizationStatus authorizationStatus= [CLLocationManager authorizationStatus];

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

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

}

根据您的应用,您可能不希望在启动时询问用户的许可,因为不建议这样做。

答案 1 :(得分:4)

错误消息非常直观。在获得授权之前,请勿致电[self.locationManager startUpdatingLocation]。根据文档,您的[self.locationManager requestWhenInUseAuthorization]是异步的。

  

当前授权状态为kCLAuthorizationStatusNotDetermined时,此方法以异步方式运行,并提示用户授予应用程序使用位置服务的权限。

这意味着您既可以提示访问,也可以同时开始扫描。

相反,尝试实施-[CLLocationManagerDelegate locationManager:didChangeAuthorizationStatus:]并在确定您已获得授权后开始扫描。

答案 2 :(得分:3)

在swift:

  let locationManager: CLLocationManager = CLLocationManager()
  let authorizationStatus = CLLocationManager.authorizationStatus()


  override func viewDidLoad() {
    super.viewDidLoad()
    if(authorizationStatus == .AuthorizedWhenInUse || authorizationStatus == .AuthorizedAlways) {
       locationManager.startUpdatingLocation()
    }
    else
    {
      locationManager.requestWhenInUseAuthorization()
    }
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters

}

不要忘记将这些密钥添加到 Info.plist 文件中:

enter image description here

答案 3 :(得分:1)

信息文件中的关键字是(&#34; In&#34;而不是&#34; Is&#34;):

NSLocationWhenInUseUsageDescription

答案 4 :(得分:0)

这种方式在Xcode6上正常运行:

<强> AppDelegate.h

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

@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) CLLocationManager *locationManager;
@property (strong, nonatomic) UIWindow *window;

@end 

<强> AppDelegate.m

#import "AppDelegate.h"

@implementation AppDelegate

@synthesize locationManager = _locationManager;


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary     *)launchOptions
 {
//Current position
    self.locationManager = [[CLLocationManager alloc] init];
    [self.locationManager requestWhenInUseAuthorization];

return YES;
}
@end

第一次打开应用时会显示警告消息

答案 5 :(得分:0)

根据杰罗姆的回应,我将对我有用的答案翻译成斯威夫特。

    let authorizationStatus = CLLocationManager.authorizationStatus()

    if(authorizationStatus == .AuthorizedWhenInUse || authorizationStatus == .AuthorizedAlways) {
           self.findMyLocation()
    }

注意:根据我在文档中看到的内容,Swift中没有kCLAuthorizationStatusAuthorized等效内容。