xcode CoreLocation:didUpdateToLocation 没有被调用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11126426/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:38:58  来源:igfitidea点击:

CoreLocation: didUpdateToLocation not getting called

objective-ciosxcodecore-location

提问by Tobias Timpe

I'm currently developing my first CoreLocation-based app and I have a problem with the CLLocationManager not calling delegate methods. Here's the class I'm using:

我目前正在开发我的第一个基于 CoreLocation 的应用程序,我遇到了 CLLocationManager 不调用委托方法的问题。这是我正在使用的课程:

//
//  LocationGetter.h
//  whatsunderme
//
//  Created by Tobias Timpe on 16.06.12.
//  Copyright (c) 2012 tobisoft. All rights reserved.
//

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
@protocol LocationGetterDelegate
@required
- (void) newPhysicalLocation:(CLLocation *)location;
@end

@interface LocationGetter : NSObject <CLLocationManagerDelegate> {
   CLLocationManager *locationManager;
   id delegate;
} 

- (void)startUpdates;

@property (nonatomic, retain) CLLocationManager *locationManager;
@property (nonatomic, retain) id delegate;
@end

//
//  LocationGetter.m
//  whatsunderme
//
//  Created by Tobias Timpe on 16.06.12.
//  Copyright (c) 2012 tobisoft. All rights reserved.
//

#import "LocationGetter.h"

@implementation LocationGetter

@synthesize locationManager, delegate;
BOOL didUpdate = NO;

- (void)startUpdates {
    if (self.locationManager == nil) {
        self.locationManager = [[CLLocationManager alloc] init];
    }
    self.locationManager.delegate = self;
    NSLog(@"Starting Location Updates");
    locationManager.distanceFilter = 100;
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [self.locationManager startUpdatingLocation];
}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    NSLog(@"Error");
}

// Delegate method from the CLLocationManagerDelegate protocol.
- (void)locationManager:(CLLocationManager *)manage didUpdateToLocation:(CLLocation *)
newLocation fromLocation:(CLLocation *)oldLocation {
        NSLog(@"new Location found");
    didUpdate = YES;
    // Disable future updates to save power.
    [self.locationManager stopUpdatingLocation];

    [self.delegate newPhysicalLocation:newLocation];

}


@end

Neither the didUpdateToLocation nor the didFailWithError methods are getting called. I don't know why. Hope you can help me with this since I've been trying to figure out what the problem is for 2 hours.

didUpdateToLocation 和 didFailWithError 方法都没有被调用。我不知道为什么。希望你能帮我解决这个问题,因为我一直试图找出问题所在 2 个小时。

回答by ElasticThoughts

Use my code, it should work for you...

使用我的代码,它应该适合你......

First ensure you are using the protocol by adding the following, for example to your AppDelegate or whichever controller you want CoreLocation to work in...

首先通过添加以下内容来确保您正在使用该协议,例如添加到您的 AppDelegate 或您希望 CoreLocation 在其中工作的任何控制器...

@interface AppDelegate : UIResponder <UIApplicationDelegate, CLLocationManagerDelegate>

Create some properties, I also use a custom method but you don't have too...

创建一些属性,我也使用自定义方法,但您没有太多...

@property (strong, nonatomic) CLLocationManager *locationManager;
@property (strong, nonatomic) CLLocation *currentLocation;

- (void)startUpdatingCurrentLocation;

Then in your implementation file call the custom method to start CL

然后在您的实现文件中调用自定义方法来启动 CL

// start location services
[self startUpdatingCurrentLocation];

Below is the code to my custom method.

下面是我的自定义方法的代码。

- (void)startUpdatingCurrentLocation
{    
  // if location services are on
  if([CLLocationManager locationServicesEnabled])
  {
    // if location services are restricted do nothing
    if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied || [CLLocationManager authorizationStatus] == kCLAuthorizationStatusRestricted )
    {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Warning" message:@"Determining your current location cannot be performed at this time because location services are enabled but restricted." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alertView show];
    }
    else 
    {
        if(!locationManager_)
        {
            locationManager_ = [[CLLocationManager alloc] init];
            [locationManager_ setDesiredAccuracy:kCLLocationAccuracyBest];
            [locationManager_ setDelegate:self];
            [locationManager_ setDistanceFilter:5.0f];          // measured in meters
            [locationManager_ setHeadingFilter:5];              // measured in degrees
            [locationManager_ setPurpose:@"Enabling location services is required in order to automatically determine the location of yada yada yada..."];
        }

        [locationManager_ startUpdatingLocation];
    }
  }
  else 
  {
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Determining your current location cannot be performed at this time because location services are not enabled." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alertView show];
  }
}

回答by Anum

For me adding the string "location-services" to UIRequiredDeviceCapabilities array in Info.plist solved the problem.

对我来说,将字符串“location-services”添加到 Info.plist 中的 UIRequiredDeviceCapabilities 数组解决了这个问题。

Read the documentation here https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/LocationAwarenessPG/CoreLocation/CoreLocation.html#//apple_ref/doc/uid/TP40009497-CH2-SW1

在此处阅读文档 https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/LocationAwarenessPG/CoreLocation/CoreLocation.html#//apple_ref/doc/uid/TP40009497-CH2-SW1

The value for the UIRequiredDeviceCapabilities is an array of strings indicating the features that your app requires. There are two strings relevant to location services:

UIRequiredDeviceCapabilities 的值是一个字符串数组,指示您的应用程序需要的功能。有两个与定位服务相关的字符串:

Include the location-services string if you require location services in general. Include the gps string if your app requires the accuracy offered only by GPS hardware.

如果您一般需要定位服务,请包含定位服务字符串。如果您的应用程序需要仅由 GPS 硬件提供的精度,请包含 gps 字符串。

回答by Tobias Timpe

Duh, stupid me!

哼,我傻了!

I forgot to declare the variable for the LocationGetter instance in the .h file instead of the .m file!

我忘了在 .h 文件而不是 .m 文件中声明 LocationGetter 实例的变量!