提示在iOS8中进行位置授权

时间:2015-01-15 07:37:33

标签: ios8 cllocationmanager

我在本网站,苹果开发网站和无数的YouTube视频上看过其他类似的问题,但我仍然无法弄清楚这一点。关于在Xcode / iOS8中获取用户位置的信息似乎很少。

我有一个地图视图,工作正常,除非我点击一个按钮获取我的位置,没有任何反应,在我看到的日志中

  

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

我不明白我做错了什么。我对编程很陌生,所见的所有信息对我来说都没有多大意义。

我在我的应用中使用了故事板,具体的代码如下:

mapViewController.h

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
@interface mapViewController : UIViewController  {
MKMapView *mapview;
}
@property (nonatomic, retain) IBOutlet MKMapView *mapview;
-(IBAction)setMap:(id)sender;
-(IBAction)getCurrentLocation:(id)sender;
@property (nonatomic, retain) IBOutlet CLLocationManager *locationManager;
@end

mapViewController.m

#import "mapViewController.h"
@interface mapViewController ()
@end
@implementation mapViewController {
CLLocationManager *locationManager;
}
@synthesize mapview;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.locationManager.delegate=self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.requestWhenInUseAuthorization;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(IBAction)setMap:(id)sender {
switch (((UISegmentedControl *) sender).selectedSegmentIndex) {
    case 0:
        mapview.mapType = MKMapTypeStandard;
        break;
    case 1:
        mapview.mapType = MKMapTypeSatellite;
        break;
    case 2:
        mapview.mapType = MKMapTypeHybrid;
        break;
       default:
        break;
}
}
-(IBAction)getCurrentLocation:(id)sender {
mapview.showsUserLocation = YES;
}
@end

我一直在努力解决这个问题,我把头发撕掉了。有人可以指出我出错的地方吗?

1 个答案:

答案 0 :(得分:0)

错误信息非常自我解释。

首先,您没有在CLLocationManager中初始化viewDidLoad。您可以使用(delegate行之前)初始化:

self.locationManager = [[CLLocationManager alloc] init];

然后,您必须使用[self.locationManager requestWhenInUseAuthorization][self.locationManager requestAlwaysAuthorization]请求位置服务。 (不要忘记在Info.plist)中添加相应的使用说明密钥。

最后,您必须检查用户授权的响应。如果用户拒绝授权,则在再次询问之前,不应启动任何与位置服务相关的操作。


更新如果您也支持iOS 7,则代码会略有不同。

相关问题