【问题标题】:iOS 8 MKMapView User Location Request FailureiOS 8 MKMapView 用户位置请求失败
【发布时间】:2014-09-15 12:26:15
【问题描述】:

我一直在尝试使用 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;

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;
}

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

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

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

更新 2: 这是我实际成功处理用户请求的设置中的 SS,但无论我刷新多少次都无法获取/更新位置。
(来源:barisaltop.com)

谢谢!

【问题讨论】:

  • 我也有同样的问题。你找到解决这个问题的方法了吗?

标签: ios objective-c mkmapview ios8 cllocationmanager


【解决方案1】:

几天前我遇到了同样的问题。解决方案是将string 键NSLocationAlwaysUsageDescription(用于[CLLocationManager requestAlwaysAuthorization])或NSLocationWhenInUseUsageDescription(用于[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>

希望这会有所帮助。

【讨论】:

  • 不幸的是,我已经这样做了,并在我原来的问题中提到了它。经过进一步审查,我怀疑这是一个方案或 gpx 文件错误。我将在 iOS8 机器上测试它并更新我的问题。
  • Ups,我错过了那部分。对不起! :)
【解决方案2】:

我终于成功地在模拟器上运行了我的 gpx 文件。似乎在第一次安装 Xcode 6 后,可能存在导致 gpx 文件模拟的错误。这是我克服问题的方法:

  1. 我已从模拟器中删除了我的应用
  2. 在应用下->功能启用后台模式->位置更新
  3. 运行应用并让它安装在模拟器上
  4. 允许访问,我能够使用 GPX 找到用户
  5. 之后我禁用了位置更新。

我不知道为什么,但这对我有用。

【讨论】:

  • 我也有同样的问题。无法让它在我的 iPod touch 中工作。可能是什么原因?
猜你喜欢
  • 2015-09-23
  • 2014-11-18
  • 2015-01-29
  • 2015-04-02
  • 2014-11-17
  • 2017-02-13
  • 2013-11-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多