簡體   English   中英

位置管理器在 iOS8 中不起作用

[英]Location manager is not working in iOS8

請不要將其標記為重復,因為我從其他人在堆棧溢出中發布的答案中獲取了幫助。 但我仍然面臨一些問題。 即使在真實設備中也不會調用位置管理器委托。 而且它也沒有請求許可。

下面是我的代碼。

- (IBAction)getUserLocationAction:(id)sender
{
    if([CLLocationManager locationServicesEnabled])
    {
        if(!locationManager)
            locationManager = [[CLLocationManager alloc] init];

        [locationManager setDelegate:self];
        [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];

        if ([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
            [locationManager requestWhenInUseAuthorization];
        }

        [locationManager startUpdatingLocation];
        //[locationManager startMonitoringSignificantLocationChanges];
    }
    else
    {
        NSLog(@"Location service is not enabled");
    }
}


#pragma mark - Location Manager Delegate- 

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    NSLog(@"%@", locations);
    userLocation = [locations lastObject];
    NSLog(@"User Current Locatiion\nLat: %+.6f\nLong: %+.6f", [userLocation coordinate].latitude, [userLocation coordinate].longitude);

   [locationManager stopUpdatingLocation];
   //[locationManager stopMonitoringSignificantLocationChanges];
}



- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    NSLog(@"Error: %@", [error localizedDescription]);
}

我還在我的 .plist 文件中添加了以下兩個鍵

<key>NSLocationWhenInUseUsageDescription</key>
<string>This app want to use your location</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>This app want to use your location</string>

我還添加了框架

在此處輸入圖片說明

我不確定我哪里做錯了。 請幫我。

更新

首先,我認為這是我的代碼的一個問題,但經過一些研究,它在 iOS7 中按預期工作。 代碼保持不變,此問題僅在iOS8中出現。 請讓我知道在 iOS8 中工作還需要做哪些更改。

你需要做更多的檢查:

- (IBAction)getUserLocationAction:(id)sender
{
    if([CLLocationManager locationServicesEnabled]) {

        if(!self.locationManager) {
            self.locationManager = [[CLLocationManager alloc] init];
            [self.locationManager setDelegate:self];
            [self.locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
        }

        CLAuthorizationStatus authStatus = [CLLocationManager authorizationStatus];
        if (authStatus == kCLAuthorizationStatusNotDetermined) {
            // Check for iOS 8 method
            if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
                [self.locationManager requestWhenInUseAuthorization];
            }
            else {
                [self.locationManager startUpdatingLocation];
            }
        }
        else if(authStatus == kCLAuthorizationStatusAuthorizedAlways || authStatus == kCLAuthorizationStatusAuthorizedWhenInUse || authStatus == kCLAuthorizationStatusAuthorized) {
            [self.locationManager startUpdatingLocation];
        }
        else if(authStatus == kCLAuthorizationStatusDenied){
            NSLog(@"User did not allow location tracking.");
            // present some dialog that you want the location.
        }
        else {
            // kCLAuthorizationStatusRestricted
            // restriction on the device do not allow location tracking.
        }
    }
    else {
        NSLog(@"Location service is not enabled");
    }
}

您還需要處理使用回調:

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
    if (status != kCLAuthorizationStatusRestricted &&  status !=kCLAuthorizationStatusDenied) {
        [self.locationManager startUpdatingLocation];
    }
}

檢查您的位置訪問設置..所以轉到設置-->隱私-->定位服務-->您的應用

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM