简体   繁体   English

CLLocationManager不会向didUpdateLocations方法发送位置

[英]CLLocationManager does not send location to the didUpdateLocations method

I am working on an iOS 6.1 iPhone app with ARC enabled which will be using a CLLocationManager to track the devices position. 我正在开发支持ARCiOS 6.1 iPhone app ,它将使用CLLocationManager来跟踪设备位置。

UPDATE UPDATE

I have a DestinationViewController which implements the CLLocationManagerDelegate . 我有一个DestinationViewController实现CLLocationManagerDelegate It creates the CLLocationManager and does the setup. 它创建CLLocationManager并进行设置。 I have imported the CoreLocation framework to my project. 我已将CoreLocation framework导入到我的项目中。

I have tried to debug a lot and can see that the CLLocationManager is created and no errors are present. 我试图调试很多,可以看到创建了CLLocationManager并且没有错误。 But the problem is that the [CLLocationManager authorizationStatus] is kCLAuthorizationStatusNotDetermined both before [self.locationManager startUpdatingLocation]; 但问题是[CLLocationManager authorizationStatus]kCLAuthorizationStatusNotDetermined两者之前[self.locationManager startUpdatingLocation]; and also after . 以及之后 So there is no prompt about asking permission to use location services for this app. 因此,没有提示要求为此应用程序使用位置服务的权限。 Because of that this method is never fired. 因此,这种方法永远不会被解雇。

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:  
(NSArray *)locations

I have also tried to search the web for hours to look for similar examples but with no luck. 我也尝试在网上搜索几个小时来寻找类似的例子,但没有运气。 Code from DestinationViewController is posted below. DestinationViewController中的代码发布在下面。

Interface 接口

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>

@interface DestinationViewController : UIViewController <CLLocationManagerDelegate>

Implementation 履行

#import "DestinationViewController.h"

@interface DestinationViewController ()
@property (strong, nonatomic) CLLocationManager *locationManager;
@end


@implementation DestinationViewController

// Initializer
- (CLLocationManager *)locationManager
{
    if (!_locationManager) {
        _locationManager = [[CLLocationManager alloc] init];
    }
    return _locationManager;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self startLocation];
}


- (void)startLocation
{
    self.locationManager.delegate = self;
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    self.locationManager.distanceFilter = 1;

    NSString *error;
    if (![CLLocationManager locationServicesEnabled]) {
        error = @"Error message";
    }

    CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
    if (status == kCLAuthorizationStatusRestricted ||
        status == kCLAuthorizationStatusDenied ||) {
        error = @"Error message";
    }

    if (error) {
        NSLog(error);
    }
    else
    {

        status = [CLLocationManager authorizationStatus];

    self.locationManager.pausesLocationUpdatesAutomatically = NO;

        [self.locationManager startUpdatingLocation];

        status = [CLLocationManager authorizationStatus];

        NSLog(@"CLLocationManager is %@", self.locationManager);
        NSLog(@"Location is %@", self.locationManager.location);

        [self updateUI];
    }
}


- (void)locationManager:(CLLocationManager *)manager
     didUpdateLocations:(NSArray *)locations
{
    CLLocation *recentLocation = [locations lastObject];
    NSLog(@"Found location");
}

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

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
    NSLog(@"Change in authorization status");
}

Your code should work. 你的代码应该有效。

Make sure your application is authorized to use location services. 确保您的应用程序有权使用位置服务。

You can use following method to check authorization status in code 您可以使用以下方法检查代码中的授权状态

 + (CLAuthorizationStatus)authorizationStatus

Edit 编辑

kCLAuthorizationStatusNotDetermined is most likely caused by location services being disabled. kCLAuthorizationStatusNotDetermined很可能是由禁用位置服务引起的。 You should check location services status first. 您应该首先检查位置服务状态。 Code should be like this 代码应该是这样的

if([CLLocationManager locationServicesEnabled]) {
    // Location Services Are Enabled
    switch([CLLocationManager authorizationStatus]) {
        case kCLAuthorizationStatusNotDetermined:
            // User has not yet made a choice with regards to this application
            break;
        case kCLAuthorizationStatusRestricted:
            // This application is not authorized to use location services.  Due
            // to active restrictions on location services, the user cannot change
            // this status, and may not have personally denied authorization
            break;
        case kCLAuthorizationStatusDenied:
            // User has explicitly denied authorization for this application, or
            // location services are disabled in Settings
            break;
        case kCLAuthorizationStatusAuthorized:
            // User has authorized this application to use location services
            break;
    }
} else {
    // Location Services Disabled
}

I finally solved it! 我终于解决了! My delegate wasn't set properly. 我的代表没有正确设置。 Instead of doing: 而不是做:

self.locationManager.delegate = self;

I changed it to: 我改成了:

[self.locationManager setDelegate:self];

And now my delegate methods get fired. 现在我的委托方法被解雇了。

Thanks to those who helped! 感谢那些帮助过的人!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM