简体   繁体   English

将requestWhenInUseAuthorization添加到plist文件后,iOS地图视图给出错误

[英]iOS map view is giving error after adding requestWhenInUseAuthorization into plist file

When trying to display of current location on the map after adding the requestWhenInUseAuthorization into the plist file, still I'm getting error: 将requestWhenInUseAuthorization添加到plist文件后尝试在地图上显示当前位置时,仍然出现错误:

Trying to start MapKit location updates without prompting for location authorization. Must call -[CLLocationManager requestWhenInUseAuthorization] or -[CLLocationManager requestAlwaysAuthorization] first.

I tried to solve several ways. 我试图解决几种方法。 But still failed. 但是还是失败了。 I am confused which part I am missing. 我很困惑我缺少哪一部分。 My full code is: 我的完整代码是:

I added a mapView on the top of view controller and then mapped it with .h file. 我在视图控制器的顶部添加了mapView,然后将其与.h文件映射。

.h file code: .h文件代码:

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

@interface ViewController : UIViewController <MKMapViewDelegate>

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

@end

.m file code: .m文件代码:

#import "ViewController.h"

@interface ViewController ()


@end

@implementation ViewController

@synthesize mapView = _mapView;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    [self.mapView.userLocation self];
    [self.mapView setShowsUserLocation:YES];
}


-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{
    CLLocationCoordinate2D loc = [userLocation coordinate];
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(loc, 500, 500);
    [self.mapView setRegion:region animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

Can any body please tell me which thing I am missing here. 任何人都可以告诉我我在这里想念的是什么。 I'm new in iOS and I tried to solve couple of hours. 我是iOS的新手,所以尝试解决了几个小时。

You need to call requestWhenInUseAuthorization or requestAlwaysAuthorization to prompt the user if he will allow location tracking. 您需要调用requestWhenInUseAuthorization或requestAlwaysAuthorization来提示用户是否允许位置跟踪。 Then you need to handle the response and use location only if the user allows it. 然后,只有在用户允许的情况下,您才需要处理响应并使用位置。

It should look like this: 它看起来应该像这样:

  1. Find the status of authorization 查找授权状态
  2. If the status is CLAuthorizationStatus.NotDetermined, call requestWhenInUseAuthorization or requestAlwaysAuthorization and process the result 如果状态为CLAuthorizationStatus.NotDetermined,则调用requestWhenInUseAuthorization或requestAlwaysAuthorization并处理结果
  3. If the authorization status is kCLAuthorizationStatusRestricted or kCLAuthorizationStatusDenied, your app is not permitted to use location services and you should abort your attempt to use them 如果授权状态为kCLAuthorizationStatusRestricted或kCLAuthorizationStatusDenied,则不允许您的应用使用位置服务,因此您应中止使用它们的尝试

https://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLLocationManager_Class/index.html#//apple_ref/occ/instm/CLLocationManager/requestWhenInUseAuthorization https://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLLocationManager_Class/index.html#//apple_ref/occ/instm/CLLocationManager/requestWhenInUseAuthorization

Try this, 尝试这个,

Create an object for CLLocationManager. 为CLLocationManager创建一个对象。

Add the following code inside of your viewDidLoad. 在viewDidLoad中添加以下代码。

locationManager.delegate = self; locationManager.desiredAccuracy = kCLLocationAccuracyBest; if ([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) { [locationManager requestWhenInUseAuthorization]; }

Don't forgot to add 不要忘记添加

CLLocationManagerDelegate

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 IOS 7.1上的requestWhenInUseAuthorization错误 - requestWhenInUseAuthorization error on IOS 7.1 iOS 9 [locationManager.requestWhenInUseAuthorization()]警报视图在出现后迅速消失 - iOS 9 [locationManager.requestWhenInUseAuthorization()] alert view disappears quickly after appearing 即使在 info.plist 文件 swift iOS 中添加 NSMicrophoneUsageDescription 后,应用程序也会崩溃 - App crash even after adding NSMicrophoneUsageDescription in info.plist file swift iOS Cordova ios 添加扩展 - 错误:找不到 -Info.plist 文件或 config.xml 文件 - Cordova ios adding extension - Error: could not find -Info.plist file, or config.xml file 即使在plist文件上添加了临时异常之后,xcode 9.3版ios 11中的HTTP加载也会失败 - HTTP load failed in xcode version 9.3 ios 11 even after adding temporary exception on plist file 将Firebase项目添加到iOS应用以下载.plist文件时发生未知错误 - unknown error while adding Firebase project to iOS app to download .plist file iOS-将项目添加到plist - iOS - Adding items to plist Cocos2d:.plist文件给我一个SIGABRT错误 - Cocos2d: .plist file giving me a SIGABRT error requestAlwaysAuthorization after requestWhenInUseAuthorization被接受 - requestAlwaysAuthorization after requestWhenInUseAuthorization is accepted 在iOS中将键值对添加到plist文件时出现问题 - Problems when adding key-value pair to plist file in iOS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM