显示用户的位置Xcode 6 / IOS 8

时间:2014-10-04 21:04:01

标签: geolocation ios8 xcode6 core-location

我已经更新到Xcode 6(来自Xcode 5),现在我的应用程序不再工作了(我很自豪它在IOS7下工作)。我有这个“着名的”调试输出:

  

尝试在不提示位置授权的情况下启动MapKit位置更新。必须首先调用 - [CLLocationManager requestWhenInUseAuthorization]或 - [CLLocationManager requestAlwaysAuthorization]。

当然,我一直在谷歌搜索这个消息以找到解决方案,但似乎没有任何工作。所以我在征求你的意见。

这是我的头文件:

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

#define kGOOGLE_API_KEY @"my google api"
#define kBgQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)

@interface XYZViewController : UIViewController <MKMapViewDelegate,       CLLocationManagerDelegate>

{
CLLocationManager *locationManager;
CLLocationCoordinate2D currentCentre;
int currenDist;
BOOL firstLaunch;
}

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

@end

这是我的实施文件:

#import "XYZViewController.h"
#import "DetailViewController.h"

@interface XYZViewController ()

@end

@implementation XYZViewController

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

//Make this controller the delegate for the map view.
self.mapView.delegate = self;

// Ensure that you can view your own location in the map view.
[self.mapView setShowsUserLocation:YES];

//Instantiate a location object.
locationManager = [[CLLocationManager alloc] init];

//Make this controller the delegate for the location manager.
[locationManager setDelegate:self];

//Set some parameters for the location object.
[locationManager setDistanceFilter:kCLDistanceFilterNone];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];

firstLaunch=YES;
}

1 个答案:

答案 0 :(得分:0)

您需要按照错误说明告诉您,并添加对requestWhenInUseAuthorizationrequestAlwaysAuthorization的通话。这已经包括在内,即。 iOS alertview for location permission doesn't pop up

所需的更改可能如下所示:

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

  //Make this controller the delegate for the map view.
  self.mapView.delegate = self;


  //Instantiate a location object.
  locationManager = [[CLLocationManager alloc] init];

  //Make this controller the delegate for the location manager.
  [locationManager setDelegate:self];

  //Set some parameters for the location object.
  [locationManager setDistanceFilter:kCLDistanceFilterNone];
  [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];

  // Request use on iOS 8
  if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_7_1) {
    // Ensure that you can view your own location in the map view.
    [self.mapView setShowsUserLocation:YES];
  } else {
    [locationManager requestWhenInUseAuthorization];
  }

  firstLaunch=YES;
}

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
  if (status == kCLAuthorizationStatusAuthorizedWhenInUse) {
    // Ensure that you can view your own location in the map view.
    [self.mapView setShowsUserLocation:YES];
  }
}

上面的代码做了一个版本检查(以苹果推荐的方式),你也可以做一个功能检查,这是更干净,但有时会导致我自己的经验出现问题。

棘手的部分是您需要在应用Info.plist中提供您的使用说明。否则UIAlert将不会出现,您将不会获得许可。转到xcode中的项目/目标设置,单击“信息”选项卡。单击+按钮。对于密钥输入NSLocationWhenInUseUsageDescription,该值应该是一个字符串,描述您将如何使用用户的位置。您还可以打开“支持文件”下的“Info.plist”文件。

我怀疑这就是为什么它还没对你起作用。