iOS 8 MKMapView用户位置请求失败 [英] iOS 8 MKMapView User Location Request Failure

查看:121
本文介绍了iOS 8 MKMapView用户位置请求失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用MKMapview移动我的iOS7应用程序以支持iOS8。但是,我无法获得用户共享其位置以正常工作的新请求。我在故事板上创建我的MKMapView,并且代理设置并在iOS7上完美运行。以下是我为支持iOS8位置共享而添加的内容:

I have been trying to move my iOS7 app with MKMapview to support iOS8. However I couldn't get the new request for users to share their locations to work properly. I create my MKMapView on a storyboard and the delegate is set and works perfectly on iOS7. Here is what I've added to support iOS8 Location sharing:

myMapView.h

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

@interface myMapView : UIViewController <MKMapViewDelegate, CLLocationManagerDelegate>

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

myMapView.m

//Code omitted
#define IS_OS_8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
//Code omitted
- (void)viewDidLoad {
    [super viewDidLoad];
    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;

    if(IS_OS_8_OR_LATER) {
        //[self.locationManager requestWhenInUseAuthorization];
        [self.locationManager requestAlwaysAuthorization];
        [self.locationManager startUpdatingLocation];
    }
    [self.mapView setShowsUserLocation:YES];
    [self.mapView setUserTrackingMode:MKUserTrackingModeFollow animated:YES];
}

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
    MKCoordinateRegion region = { { 0.0, 0.0 }, { 0.0, 0.0 } };
    region.center.latitude = self.locationManager.location.coordinate.latitude;
    region.center.longitude = self.locationManager.location.coordinate.longitude;
    region.span.latitudeDelta = 0.0187f;
    region.span.longitudeDelta = 0.0137f;
    [self.mapView setRegion:region animated:YES];

    _initialPosition = NO;
}

我也设置了 NSLocationAlwaysUsageDescription 密钥及其在我的InfoPlist中的值,它在提示用户分享他们的位置时显示正确的消息。

Also I have set NSLocationAlwaysUsageDescription key and its value in my InfoPlist, which shows the correct message when prompting the user to share their location.

不幸的是委托函数 - (void)locationManager :( CLLocationManager *)manager didUpdateLocations :( NSArray *)locations 永远不会被调用。虽然每次加载viewController时都会调用 [self.locationManager startUpdatingLocation] ,但委托似乎没有响应它。我是如何设置代表的问题还是我在这里缺少其他东西?

Unfortunately the delegate function -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations never gets called. Although each time the viewController gets loaded the [self.locationManager startUpdatingLocation] is called, but the delegate does not seem to respond to it. Is there a problem of how I set the delegate or is there something else I am missing here?

更新:这似乎也是我的gpx启动时未调用该文件。我已清除并重新加载我的位置文件,甚至更改为默认位置,但未找到位置: Domain = kCLErrorDomain Code = 0

UPDATE: It seems also that my gpx file is not being called on launch. I have cleared and reloaded my location file, even changed to a default location, but no location is found: Domain=kCLErrorDomain Code=0

UPDATE 2:这是我用户请求实际成功的设置中的SS,但无论刷新多少都无法获取/更新位置。
应用隐私级别:位置共享总是http://barisaltop.com/location.png

UPDATE 2: Here is a SS from the settings that I have actually succeeded with the user request, but fail to get/update location no matter how much I refresh. App privacy level: Location sharing always http://barisaltop.com/location.png

谢谢!

推荐答案

我几天遇到同样的问题前。解决方案是添加字符串 NSLocationAlwaysUsageDescription (对于 [CLLocationManager requestAlwaysAuthorization] )或 NSLocationWhenInUseUsageDescription (对于 [CLLocationManager requestWhenInUseAuthorization] )到支持文件/ Info.plist

I had the same problem a few days ago. The solution was adding the string keys NSLocationAlwaysUsageDescription (for [CLLocationManager requestAlwaysAuthorization]) or NSLocationWhenInUseUsageDescription (for [CLLocationManager requestWhenInUseAuthorization]) to your Supporting Files/Info.plist

您还可以使用右键单击编辑 Info.Plist 的源代码>打开>源代码并添加以下行:

You can also edit the source code of the Info.Plist with Right click > open as > Source code and add these lines:

<!-- for requestAlwaysAuthorization -->
<key>NSLocationAlwaysUsageDescription</key>
<string>Explain for what are you using the user location</string>
<!-- for requestWhenInUseAuthorization -->
<key>NSLocationWhenInUseUsageDescription</key>
<string>Explain for what are you using the user location</string>

希望这会有所帮助。

这篇关于iOS 8 MKMapView用户位置请求失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆