問題描述:
在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];
}