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

來源:互聯網
上載者:User

標籤:des   style   blog   http   io   color   ar   os   使用   

問題描述:

在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設定如下:

允許效果:

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.