您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iOS 開發手記:iOS 8 定位服務的相容處理

編輯:iOS 開發綜合

問題描述:在 iOS 8 之前,App 第一次啟動定位服務時,系統會彈出一個提示框讓使用者選擇是否允許使用定位資訊。但到了 iOS 8,這個彈窗不再出現。第一次執行後,到「設定 → 隱私 → 定位服務」中會發現你的 App 完全沒有任何設定,既不是「永不」,也不是「使用 App 期間」。

原本可能會寫成這樣:

#import "XYZFirstViewController.h"

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

@implementation XYZFirstViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [[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];
}

-(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);
}

- (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

解決方案:以上程式碼在 iOS 8 之後,必須手動呼叫 CLLocationManager 的 requestAlwaysAuthorization/requestWhenInUseAuthorization 方法。呼叫該方法之前,還需要在 Info.plist 中設定 NSLocationAlwaysUsageDescription/NSLocationWhenInUseUsageDescription 的值,這個值會顯示在系統提示框中。

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

換句話說,iOS 8 把「要求權限」從 startUpdatingLocation 的隱式行為中拆了出來,變成開發者必須主動致電的明確步驟;而 Info.plist 中的用途說明字串則是必填項,缺少它 App 會直接崩潰。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved