在 8.1 中未调用 CLLocationManagerDelegate 委托 iOS

CLLocationManagerDelegate delegate not called iOS in 8.1

当我在 iOS 7 设备上 运行 时,我在我的静态库中使用 CLLocationManagerDelegate 一切正常,但是当我在另一个设备上使用 iOS 8.1.3 测试委托方法没有被调用。 我做了一个强大的 属性 @属性(非原子,强)CLLocationManager locationManager 我还在 info.plist 这是我的实例

self.locationManager = [[CLLocationManager alloc] init];
[self.locationManager setDelegate:self];
self.locationManager.distanceFilter = kCLDistanceFilterNone; 
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[self.locationManager startUpdatingLocation];
[self.locationManager requestWhenInUseAuthorization];
[self.locationManager requestAlwaysAuthorization];

if([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
}
[self.locationManager startUpdatingLocation];

请帮我找出为什么委托方法没有被调用!! 为什么我收不到位置更新??

希望对您有所帮助。在 if().....

中调用 [locationManager requestWhenInUseAuthorization]
if ([CLLocationManager locationServicesEnabled])
{
    locationManager = [[CLLocationManager alloc] init];

    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    locationManager.distanceFilter = kCLDistanceFilterNone;
    if ([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])
    {
        [locationManager requestWhenInUseAuthorization];
    }
    [locationManager startUpdatingLocation];
}
else{

    UIAlertView *servicesDisabledAlert = [[UIAlertView alloc] initWithTitle:@"Location Services Disabled" message:@"You currently have all location services for this device disabled. If you proceed, you will be showing past informations. To enable, Settings->Location->location services->on" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:@"Continue",nil];
    [servicesDisabledAlert show];
    [servicesDisabledAlert setDelegate:self];
}


- (void)requestWhenInUseAuthorization
{
CLAuthorizationStatus status = [CLLocationManager authorizationStatus];

// If the status is denied or only granted for when in use, display an alert
if (status == kCLAuthorizationStatusAuthorizedWhenInUse || status == kCLAuthorizationStatusDenied) {
    NSString *title;
    title = (status == kCLAuthorizationStatusDenied) ? @"Location services are off" : @"Background location is not enabled";
    NSString *message = @"To use background location you must turn on 'Always' in the Location Services Settings";

    UIAlertView *alertViews = [[UIAlertView alloc] initWithTitle:title
                                                        message:message
                                                       delegate:self
                                              cancelButtonTitle:@"Cancel"
                                              otherButtonTitles:@"Settings", nil];
    [alertViews show];
}
// The user has not enabled any location services. Request background authorization.
else if (status == kCLAuthorizationStatusNotDetermined) {
    [locationManager requestWhenInUseAuthorization];
}
}


if ([alertView.message isEqualToString:@"To use background location you must turn on 'Always' in the Location Services Settings"])
{
    if (buttonIndex == 1)
    {
        // Send the user to the Settings for this app
        NSURL *settingsURL = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
        [[UIApplication sharedApplication] openURL:settingsURL];
    }
}

必须添加 NSLocationAlwaysUsageDescriptionNSLocationWhenInUseUsageDescription 在您的 Plist 文件中带有一些消息 "Your location is needed for this app"

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {

CLLocation *location;
location =  [manager location];
CLLocationCoordinate2D coordinate = [location coordinate];;

globalObjects.longitude = [NSString stringWithFormat:@"%f",coordinate.longitude];
globalObjects.latitude = [NSString stringWithFormat:@"%f",coordinate.latitude];
}

对于iOS 8 你需要在Info.plist.

中定义"Privacy - Location Usage Description"

例如。隐私 - 位置使用说明 = "Use your location to show near by stores".

此键指定访问用户位置信息的原因。

locationManager = [[CLLocationManager alloc] init];

[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[locationManager setDistanceFilter:5];
[locationManager setHeadingFilter:5];
[locationManager setDelegate:self];

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

[locationManager startUpdatingLocation];

你能在你的控制台输出中看到类似下面的信息吗?

Trying to start MapKit location updates without prompting for location authorization. Must call -[CLLocationManager requestWhenInUseAuthorization] or -[CLLocationManager requestAlwaysAuthorization] first.

我 运行 第一次将我的应用程序移植到 iOS8 时遇到了这个问题。定义以下键有新的限制。

    /* Localized versions of Info.plist keys */

"NSLocationWhenInUseUsageDescription" = "for some reason, your app will use your location whenever the app is in foreground";

"NSLocationAlwaysUsageDescription" = "for some reason, your app will use your location whenever the app is in background";

这些键必须在项目根目录中名为 InfoPlist.strings 的可本地化字符串文件中定义。我有一次把这个文件放错了位置,我花了很长时间才弄清楚问题出在哪里。

接下来您应该检查,您请求授权的 CLLocationManager 的 生命周期没有超过用户确认的时间处理。例如。在您的 AppDelegate 中定义一个全局 CLLocationManager。

希望对您有所帮助, 亲切的问候, 彼得

if (![CLLocationManager locationServicesEnabled])
    {
        [[UtilityClass sharedObject]showAlertWithTitle:@"Location Services disabled" andMessage:@"App requires location services to find your current city weather.Please enable location services in Settings."];
    }
    else{
        [self stopLocationUpdating];
        if (locationManager==nil) {
            locationManager = [[CLLocationManager alloc] init];
            locationManager.delegate = self;
            locationManager.desiredAccuracy = kCLLocationAccuracyBest;

            #ifdef __IPHONE_8_0
            if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8"))
            {
                // Use one or the other, not both. Depending on what you put in info.plist
                //[self.locationManager requestWhenInUseAuthorization];
                [locationManager requestAlwaysAuthorization];
            }
            #endif
        }
        [locationManager startUpdatingLocation];



        [locationManager startUpdatingLocation];
    }

并将以下代码添加到您的 .plist 文件中

NSLocationAlwaysUsageDescription=Application would like to use your location