IOS8上定位問題

來源:互聯網
上載者:User

標籤:ios8 定位 問題

在ios7之前,我們進入程式提示使用者開啟定位是這樣做的,如下:

        CLLocationManager * locationManager = [[CLLocationManager alloc] init];//建立位置管理器

        //locationManager.delegate=_instance;

        locationManager.desiredAccuracy=kCLLocationAccuracyBest;

        locationManager.distanceFilter=100.0f;

        //定位服務是否可用

        BOOL enable=[CLLocationManager locationServicesEnabled];

        //是否具有定位許可權

        int status=[CLLocationManager authorizationStatus];

        if(!enable || status<3){

            //請求許可權

            [locationManager requestWhenInUseAuthorization];

        }

但是到了ios8後,發現定位壓根就不起作用,老是提示:650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/4B/F2/wKioL1Q1_3Gwxq89AAEJ-etU45o752.jpg" title="016C4C99-9E8C-4FB3-A14E-F08152804E06.png" alt="wKioL1Q1_3Gwxq89AAEJ-etU45o752.jpg" />


ios8定位解決如下:

先在info.plist定義key:(總是授權)NSLocationAlwaysUsageDescription或者使用時授權NSLocationWhenInUseUsageDescription

在.h裡面

  繼承代碼CLLocationManagerDelegate

   定義CLLocationManager *_locationManager;

在.m裡面定義:

- (void)startTrackingLocation {

    CLAuthorizationStatus status = [CLLocationManager authorizationStatus];

    if (status == kCLAuthorizationStatusNotDetermined) {

        //總是授權

        [_locationManager requestAlwaysAuthorization];

        //每次授權一次

        //[_locationManager requestWhenInUseAuthorization];

    }

    else if (status == kCLAuthorizationStatusAuthorizedWhenInUse || status == kCLAuthorizationStatusAuthorizedAlways) {

        [_locationManager startUpdatingLocation];

    }

}


//實現代碼回調

#pragma mark - CLLocationManager Delegate Methods

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {

    switch (status) {

        case kCLAuthorizationStatusAuthorizedAlways:

        case kCLAuthorizationStatusAuthorizedWhenInUse:

            NSLog(@"Got authorization, start tracking location");

            [self startTrackingLocation];

            break;

        case kCLAuthorizationStatusNotDetermined:

            [_locationManager requestAlwaysAuthorization];

            break;

        default:

            break;

    }

}


調用如下:   

    if (IOS8) {

        _locationManager = [[CLLocationManager alloc] init];

        _locationManager.delegate = self;

        [self startTrackingLocation];

    }


最後實現效果如,點擊Allow即可定位了:

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/4B/F5/wKioL1Q2AjLh6ra9AAEmwOdLAiE207.jpg" title="1EA3D9CE-DECD-4F23-A751-DDC6FF68C217.png" alt="wKioL1Q2AjLh6ra9AAEmwOdLAiE207.jpg" />



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.