iOS 8 MKMapView用户位置请求失败

jay*_*_21 2 objective-c mkmapview cllocationmanager ios ios8

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

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;
Run Code Online (Sandbox Code Playgroud)

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;
}
Run Code Online (Sandbox Code Playgroud)

此外,我NSLocationAlwaysUsageDescription在InfoPlist中设置了密钥及其值,在提示用户共享其位置时显示正确的消息.

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

更新:似乎我的gpx文件在启动时没有被调用.我已清除并重新加载我的位置文件,甚至更改为默认位置,但找不到位置:Domain=kCLErrorDomain Code=0

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

谢谢!

mer*_*los 7

几天前我遇到了同样的问题.解决方案是将string密钥NSLocationAlwaysUsageDescription(for [CLLocationManager requestAlwaysAuthorization])或NSLocationWhenInUseUsageDescription(for [CLLocationManager requestWhenInUseAuthorization])添加到您的Supporting Files/Info.plist中

您还可以编辑的源代码Info.Plist右键>开放为>源代码,并添加这些行:

<!-- 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>
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助.