【问题标题】:Show user's location Xcode6 /IOS8显示用户位置 Xcode 6 /IOS 8
【发布时间】:2014-11-29 14:05:19
【问题描述】:

我已经更新到 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;
}

【问题讨论】:

    标签: geolocation ios8 xcode6 core-location


    【解决方案1】:

    您需要按照错误描述告诉您的操作,并添加对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”文件。

    我怀疑这就是为什么它还没有为你工作。

    【讨论】:

    • 我在之前的尝试中确实添加了带有字符串值的键,但没有奏效。无论如何,您在这么短的时间内回答对我帮助很大,现在我的项目正在运行!非常感谢。最好的问候,
    猜你喜欢
    • 1970-01-01
    • 2014-11-14
    • 1970-01-01
    • 2015-01-20
    • 1970-01-01
    • 1970-01-01
    • 2015-01-29
    • 2014-11-08
    • 1970-01-01
    相关资源
    最近更新 更多