iOS開發手記-iOS8中使用定位服務解決方案


問題描述:

在iOS8之前,app第一次開始定位服務時,系統會彈出一個提示框來讓用戶選擇是否允許使用定位信息。但iOS8后,app將不會出現這個彈窗。第一次運行之后,在設置->隱私->定位服務中,你的app沒有任何設置,既不是“永不”,也不是“始終”。

代碼如下:

#import "XYZFirstViewController.h"

@interface XYZFirstViewController ()
- (IBAction)LocateButtonClick:(id)sender;
@end

@implementation XYZFirstViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(startLocate) name:@"startLocateNotification" object:nil];
    _locationManager=[[CLLocationManager alloc] init];
    _locationManager.delegate=self;
    _locationManager.desiredAccuracy=kCLLocationAccuracyBest;
    _locationManager.distanceFilter=1000.0f;
    _mapView.mapType=MKMapTypeStandard;
    _mapView.delegate=self;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void) viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [_locationManager startUpdatingLocation];
}

-(void) viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [_locationManager stopUpdatingLocation];
}


-(void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    CLLocation *currentLocation=[locations lastObject];
    _currentLocation=currentLocation;
    self.currentLocationLabel.text=[NSString stringWithFormat:@"%3.5f,%3.5f,%3.5f", currentLocation.coordinate.longitude,currentLocation.coordinate.latitude,currentLocation.altitude];
    MKCoordinateRegion region=MKCoordinateRegionMakeWithDistance(currentLocation.coordinate, 1000, 1000);
    [_mapView setRegion:region animated:YES];
    MKPointAnnotation *point=[[MKPointAnnotation alloc] init];
    point.coordinate=_currentLocation.coordinate;
    point.title=@"my location";
    [_mapView addAnnotation:point];
}

-(void) locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    NSLog(@"error:%@",error);
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)LocateButtonClick:(id)sender {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"startLocateNotification" object:self ];
}

-(void) startLocate
{
    CLGeocoder *geocoder=[[CLGeocoder alloc]init];
    [geocoder reverseGeocodeLocation:_currentLocation completionHandler:^(NSArray *placeMarks, NSError *error)
     {
        if([placeMarks count]>0)
        {
            NSLog(@"%@",placeMarks);
            CLPlacemark *placeMark=placeMarks[0];
            NSDictionary *addressDictonary=placeMark.addressDictionary;
            _currentAddressLabel.text=[NSString stringWithFormat:@"%@,%@,%@",[addressDictonary objectForKey:(NSString *)kABPersonAddressStateKey],[addressDictonary objectForKey:(NSString *)kABPersonAddressCityKey],[addressDictonary objectForKey:(NSString *) kABPersonAddressStreetKey] ];
        }
     }];
    
    
    
}
@end

 

解決方案:

以上代碼在iOS8之后需要手動調用CLLocationManager對象的requestAlwaysAuthorization/

requestWhenInUseAuthorization方法。 調用該方法需要在Info.plist中設置NSLocationAlwaysUsageDescription/NSLocationWhenInUseUsageDescription的值,這個值會顯示在系統提示框中。

代碼如下:

-(void) viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [_locationManager requestWhenInUseAuthorization];
    [_locationManager startUpdatingLocation];
}

info.plist設置如下:

允許效果:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM