位置管理器无法在iOS8中运行

时间:2015-02-24 10:32:42

标签: ios iphone delegates core-location cllocationmanager

请不要将其标记为重复,因为我从其他人的帮助中发布了堆栈溢出的答案。但我仍面临一些问题。即使在真实设备中也不会调用位置管理器代理。而且它也没有要求获得许可。

以下是我的代码。

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

我还添加了框架

enter image description here

我不确定我在哪里做错了。请帮帮我。

  

更新

首先,我认为这是我的代码的一个问题,但经过一些研究,它在iOS7中按预期工作。代码保持不变,此问题仅在iOS8中出现。请告诉我在iOS8中工作需要做哪些更改。

2 个答案:

答案 0 :(得分:0)

您需要进行更多检查:

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

答案 1 :(得分:0)

检查您的位置访问设置..也可以转到设置 - &gt;隐私 - &gt;位置服务 - &gt;您的应用