iOS8更新後定位問題CLLcationManage

來源:互聯網
上載者:User

標籤:

在IOS8更新以後以前的方法CLLocationManagerDelegate不調用didUpdateLocations

iOS8修改了位置設定裡的內容,增加了一套狀態(使用中可用/通常可用),所以以前的CLLcationManage的註冊後, 
Delegate介面不響應了,研究了一上午終於可以用了

說一下我的心得

(1)添加corelocation.framework

(2)在Info.plist中加入兩個預設沒有的欄位

  • NSLocationAlwaysUsageDescription

  • NSLocationWhenInUseUsageDescription

(3)匯入標頭檔

#import <CoreLocation/CoreLocation.h>

#import <CoreLocation/CLLocationManagerDelegate.h>

 

@interface PersonViewController : UIViewController<CLLocationManagerDelegate>{    CLLocationManager* locationManager;}@property (strong, nonatomic) CLLocationManager* locationManager;@end

 

(4)執行個體化並完成代理方法

-(void)startLocation{    [self.locationManager requestAlwaysAuthorization];    if ([CLLocationManager locationServicesEnabled]&&[CLLocationManager authorizationStatus] != kCLAuthorizationStatusDenied) {        self.locationManager = [[CLLocationManager alloc] init];        [self.locationManager setDesiredAccuracy:kCLLocationAccuracyBest];        self.locationManager.delegate = self;        if (__IPHONE_8_0) {            NSLog(@"here");            self.locationManager.distanceFilter = kCLDistanceFilterNone;            [self.locationManager setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters];        }        NSLog(@"that‘s here");            [self.locationManager startUpdatingLocation];    }}- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {    switch (status) {        case kCLAuthorizationStatusNotDetermined:        if ([locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {            [locationManager requestWhenInUseAuthorization];            }            break;        default:        break;    }}//定位代理經緯度回調-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{    [locationManager stopUpdatingLocation];    NSLog(@"location ok");    CLLocation *currentLocation = [locations firstObject];    NSLog(@"%@",[NSString stringWithFormat:@"經度:%3.5f\n緯度:%3.5f",currentLocation.coordinate.latitude,currentLocation.coordinate.longitude]);    CLGeocoder * geoCoder = [[CLGeocoder alloc] init];    [geoCoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) {        for (CLPlacemark * placemark in placemarks) {                        NSDictionary *test = [placemark addressDictionary];            //  Country(國家)  State(城市)  SubLocality(區)            NSLog(@"%@", [test objectForKey:@"State"]);            _locationCity.text = [test objectForKey:@"SubLocality"];        }    }];}- (void)locationManager:(CLLocationManager *)manager       didFailWithError:(NSError *)error {        if (error.code == kCLErrorDenied) {        NSLog(@"location%ld",(long)error.code);    }}

(5)設定開始定位結束定位

- (void)viewWillAppear:(BOOL)animated{    [super viewWillAppear:animated];    [self startLocation]; // 開始定位}- (void)viewWillDisappear:(BOOL)animated{    [super viewWillDisappear:animated];    [locationManager stopUpdatingLocation]; // 停止定位}

如果還不能定位的歡迎留言交流!

iOS8更新後定位問題CLLcationManage

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.