Ios 当前位置不会在真实设备上更新
我是iOS开发新手。我的问题是,当我想尝试在真实设备上获取当前位置,但该位置不起作用时,会出现以下错误:Ios 当前位置不会在真实设备上更新,ios,objective-c,mapkit,xcode-6.2,ios8.2,Ios,Objective C,Mapkit,Xcode 6.2,Ios8.2,我是iOS开发新手。我的问题是,当我想尝试在真实设备上获取当前位置,但该位置不起作用时,会出现以下错误: Trying to start MapKit location updates without prompting for location authorization. Must call -[CLLocationManager requestWhenInUseAuthorization] or -[CLLocationManager requestAlwaysAuthorization]
Trying to start MapKit location updates without prompting for location authorization. Must call -[CLLocationManager requestWhenInUseAuthorization] or -[CLLocationManager requestAlwaysAuthorization] first.
我使用的是Xcode 6.2,源代码如下:
////.h
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
@protocol LocationViewDelegate <NSObject>
-(void)sendLocationLatitude:(double)latitude
longitude:(double)longitude
andAddress:(NSString *)address;
@end
@interface HomeViewController : UIViewController
@property (nonatomic, assign) id<LocationViewDelegate> delegate;
- (instancetype)initWithLocation:(CLLocationCoordinate2D)locationCoordinate;
@end
////.m
#import "HomeViewController.h"
#import "anotation.h"
#import <MapKit/MapKit.h>
#import "LocationViewController.h"
//In ViewDidLoad
#define IS_OS_8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
static LocationViewController *defaultLocation = nil;
@interface HomeViewController () <MKMapViewDelegate,CLLocationManagerDelegate> {
CLLocationManager *locationManager;
MKMapView *_mapView;
MKPointAnnotation *_annotation;
CLLocationCoordinate2D _currentLocationCoordinate;
BOOL _isSendLocation;
}
@property (strong, nonatomic) NSString *addressString;
@end
@implementation HomeViewController
@synthesize addressString = _addressString;
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
_isSendLocation = YES;
}
return self;
}
- (instancetype)initWithLocation:(CLLocationCoordinate2D)locationCoordinate {
self = [super initWithNibName:nil bundle:nil];
if (self) {
_isSendLocation = NO;
_currentLocationCoordinate = locationCoordinate;
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
float fOSVersion=[[[UIDevice currentDevice] systemVersion] floatValue];
if(fOSVersion>=8.0) {
[locationManager requestAlwaysAuthorization];
CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
if (status == kCLAuthorizationStatusNotDetermined) {
[locationManager requestWhenInUseAuthorization];
} else {
[locationManager startUpdatingLocation];
}
}
UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
[backButton setImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
// [backButton setTitle:NSLocalizedString(@"back", @"") forState:UIControlStateNormal];
[backButton addTarget:self.navigationController action:@selector(popViewControllerAnimated:) forControlEvents:UIControlEventTouchUpInside];
UIView *backButtonView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 63, 33)];
// backButtonView.backgroundColor=[UIColor whiteColor];
backButtonView.bounds = CGRectOffset(backButtonView.bounds, 45, 5);
[backButtonView addSubview:backButton];
UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backButtonView];
[self.navigationItem setLeftBarButtonItem:backItem];
_mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
_mapView.delegate = self;
_mapView.mapType = MKMapTypeStandard;
_mapView.zoomEnabled = YES;
[self.view addSubview:_mapView];
if (_isSendLocation) {
_mapView.showsUserLocation = YES;//显示当前位置
UIButton *sendButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 60, 44)];
UIView *avatarButtonView = [[UIView alloc] initWithFrame:CGRectMake(290, 0, 63, 33)];
[sendButton setTitle:@"发送" forState:UIControlStateNormal];
[sendButton setTitleColor:[UIColor whiteColor]forState:UIControlStateNormal];
[sendButton addTarget:self action:@selector(sendLocation) forControlEvents:UIControlEventTouchUpInside];
//avatarButtonView.backgroundColor=[UIColor whiteColor];
avatarButtonView.bounds = CGRectOffset(avatarButtonView.bounds, -25, 5);
[avatarButtonView addSubview:sendButton];
UIBarButtonItem *avatarItem = [[UIBarButtonItem alloc] initWithCustomView:avatarButtonView];
[self.navigationItem setRightBarButtonItem:avatarItem];
// self.navigationItem.rightBarButtonItem.enabled = ;
[self startLocation];
}
else {
[self removeToLocation:_currentLocationCoordinate];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma marks StatusBar
- (UIStatusBarStyle)preferredStatusBarStyle {
return UIStatusBarStyleLightContent;
}
- (BOOL)prefersStatusBarHidden {
return NO;
}
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:NO];
}
-(void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
}
#pragma mark - class methods
+ (instancetype)defaultLocation {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
defaultLocation = [[LocationViewController alloc] initWithNibName:nil bundle:nil];
});
return defaultLocation;
}
#pragma mark - MKMapViewDelegate
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {
__weak typeof(self) weakSelf = self;
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder reverseGeocodeLocation:userLocation.location completionHandler:^(NSArray *array, NSError *error) {
if (!error && array.count > 0) {
CLPlacemark *placemark = [array objectAtIndex:0];
weakSelf.addressString = placemark.name;
[self removeToLocation:userLocation.coordinate];
}
}];
}
- (void)mapView:(MKMapView *)mapView didFailToLocateUserWithError:(NSError *)error
{
}
#pragma mark - public
- (void)startLocation {
if (_isSendLocation) {
self.navigationItem.rightBarButtonItem.enabled = NO;
}
}
-(void)createAnnotationWithCoords:(CLLocationCoordinate2D)coords {
if (_annotation == nil) {
_annotation = [[MKPointAnnotation alloc] init];
}
else{
[_mapView removeAnnotation:_annotation];
}
_annotation.coordinate = coords;
[_mapView addAnnotation:_annotation];
}
- (void)removeToLocation:(CLLocationCoordinate2D)locationCoordinate {
_currentLocationCoordinate = locationCoordinate;
float zoomLevel = 0.01;
MKCoordinateRegion region = MKCoordinateRegionMake(_currentLocationCoordinate, MKCoordinateSpanMake(zoomLevel, zoomLevel));
[_mapView setRegion:[_mapView regionThatFits:region] animated:YES];
if (_isSendLocation) {
self.navigationItem.rightBarButtonItem.enabled = YES;
}
[self createAnnotationWithCoords:_currentLocationCoordinate];
}
- (void)sendLocation {
if (_delegate && [_delegate respondsToSelector:@selector(sendLocationLatitude:longitude:andAddress:)]) {
[_delegate sendLocationLatitude:_currentLocationCoordinate.latitude longitude:_currentLocationCoordinate.longitude andAddress:_addressString];
}
[self.navigationController popViewControllerAnimated:YES];
}
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
if (status != kCLAuthorizationStatusNotDetermined) {
[locationManager startUpdatingLocation];
}
}
@end
//.h
#进口
#进口
@协议位置ViewDelegate
-(无效)发送位置纬度:(双)纬度
经度:(双)经度
andAddress:(NSString*)地址;
@结束
@接口HomeViewController:UIViewController
@属性(非原子,赋值)id委托;
-(instancetype)initWithLocation:(CLLocationCoordinate2D)locationCoordinate;
@结束
////m
#导入“HomeViewController.h”
#导入“anotation.h”
#进口
#导入“LocationViewController.h”
//在ViewDidLoad中
#定义为8或更高版本([[[UIDevice currentDevice]systemVersion]floatValue]>=8.0)
静态位置ViewController*默认位置=零;
@接口HomeViewController(){
CLLocationManager*locationManager;
MKMapView*\u mapView;
MKPointAnnotation*_注释;
CLLocationCoordinate2D_currentLocationCoordinate;
布尔伊森德定位;
}
@属性(强,非原子)NSString*addressString;
@结束
@HomeViewController的实现
@合成addressString=\u addressString;
-(instancetype)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil{
self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
如果(自我){
_isSendLocation=是;
}
回归自我;
}
-(instancetype)initWithLocation:(CLLocationCoordinate2D)locationCoordinate{
self=[super initWithNibName:nil bundle:nil];
如果(自我){
_isSendLocation=否;
_currentLocationCoordinate=位置坐标;
}
回归自我;
}
-(无效)viewDidLoad{
[超级视图下载];
locationManager=[[CLLocationManager alloc]init];
locationManager.delegate=self;
浮动fOSVersion=[[[UIDevice currentDevice]系统版本]浮动值];
如果(fOSVersion>=8.0){
[locationManager请求始终授权];
CLAuthorizationStatus=[CLLocationManager授权状态];
如果(状态==kCLAuthorizationStatusNotDetermined){
[locationManager请求使用授权];
}否则{
[locationManager startUpdatingLocation];
}
}
UIButton*backButton=[[UIButton alloc]initWithFrame:CGRectMake(0,0,100,44)];
[backButton setImage:[UIImage ImageName:@“back.png”]用于状态:UIControlStateNormal];
//[backButton设置标题:NSLocalizedString(@“back”,@“”)用于状态:UIControlStateNormal];
[backButton addTarget:self.navigationController操作:@selector(popViewControllerAnimated:)for ControlEvents:UIControlEventTouchUpInside];
UIView*backButtonView=[[UIView alloc]initWithFrame:CGRectMake(0,0,63,33)];
//backButtonView.backgroundColor=[UIColor whiteColor];
backButtonView.bounds=CGRectOffset(backButtonView.bounds,45,5);
[backButtonView添加子视图:backButton];
UIBarButtonItem*backItem=[[UIBarButtonItem alloc]initWithCustomView:backButtonView];
[self.navigationItem setLeftBarButtonItem:backItem];
_mapView=[[MKMapView alloc]initWithFrame:self.view.bounds];
_mapView.delegate=self;
_mapView.mapType=MKMapTypeStandard;
_mapView.ZoomeEnabled=是;
[self.view addSubview:_mapView];
如果(_isSendLocation){
_mapView.showsUserLocation=是//显示当前位置
UIButton*sendButton=[[UIButton alloc]initWithFrame:CGRectMake(0,0,60,44)];
UIView*avatarButtonView=[[UIView alloc]initWithFrame:CGRectMake(290,0,63,33)];
[发送按钮设置标题:@”发送" forState:uicontrol状态正常];
[sendButton setTitleColor:[UIColor whiteColor]用于状态:UIControlStateNormal];
[sendButton添加目标:自我操作:@selector(sendLocation)for ControlEvents:UIControlEventTouchUpInside];
//avatarButtonView.backgroundColor=[UIColor-whiteColor];
avatarButtonView.bounds=CGRectOffset(avatarButtonView.bounds,-25,5);
[虚拟按钮视图添加子视图:发送按钮];
UIBarButtonItem*avatarItem=[[UIBarButtonItem alloc]initWithCustomView:avatarButtonView];
[self.navigationItem setRightBarButtonItem:avatarItem];
//self.navigationItem.rightBarButtonItem.enabled=;
[自我震惊];
}
否则{
[自移除颜色:_currentLocationCoordinate];
}
}
-(无效)未收到记忆警告{
[超级记忆警告];
//处置所有可以重新创建的资源。
}
#pragma标记状态栏
-(UIStatusBarStyle)首选状态BarStyle{
返回UIStatusBarStyleLightContent;
}
-(BOOL)更喜欢StatusBarHidden{
返回否;
}
-(无效)视图将显示:(BOOL)动画{
[超级视图将显示:动画];
[self.navigationController设置NavigationBarHidden:否];
}
-(无效)视图将消失:(BOOL)已设置动画{
[超级视图将消失:动画];
}
#pragma标记类方法
+(instancetype)默认位置{
静态调度一次;
一次发送(一次发送)^{
defaultLocation=[[LocationViewController alloc]initWithNibName:nil bundle:nil];
});
返回默认位置;
}
#pragma标记-MKMapViewDelegate
-(void)mapView:(MKMapView*)mapView didUpdateUserLocation:(MKUserLocation*)userLocation{
__弱类型(自我)弱自我=自我;
CLGeocoder*geocoder=[[CLGeocoder alloc]init];
[geocoder reverseGeocodeLocation:userLocation.location completionHandler:^(NSArray*数组,NSError*错误){
如果(!error&&array.count>0){
CLPlacemark*placemark=[array objectAtIndex:0];
weakSelf.addressString=placemark.name;
[self-removeToLocation:userLocation.coordinate];
}
}];
}
-(void)mapView:(MKMapView*)mapView未能定位错误:(n错误*)错误
{
}
#布拉格马克-公共
-(空)惊吓{
如果(_isSendLocation){
自助导航
if (status != kCLAuthorizationStatusNotDetermined) {
[locationManager startUpdatingLocation];
}
if (status == kCLAuthorizationStatusAuthorized) {
[locationManager startUpdatingLocation];
}
CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
if (status == kCLAuthorizationStatusNotDetermined) {
[locationManager requestWhenInUseAuthorization];
} else {
[locationManager startUpdatingLocation];
}