3

我正在研究一个iOS 6.1 iPhone appwith ARCenabled ,它将使用 aCLLocationManager来跟踪设备的位置。

更新

我有一个DestinationViewController实现CLLocationManagerDelegate. 它创建CLLocationManager并进行设置。我已将其导入CoreLocation framework到我的项目中。

我尝试了很多调试,可以看到CLLocationManager已创建并且不存在错误。但问题是[CLLocationManager authorizationStatus]既是kCLAuthorizationStatusNotDetermined before[self.locationManager startUpdatingLocation];也是after。所以没有提示询问是否允许使用此应用的位置服务。因为这个方法永远不会被解雇。

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

我还尝试在网上搜索几个小时以寻找类似的示例,但没有运气。DestinationViewController 中的代码发布在下面。

界面

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

@interface DestinationViewController : UIViewController <CLLocationManagerDelegate>

执行

#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");
}
4

2 回答 2

7

您的代码应该可以工作。

确保您的应用程序被授权使用位置服务。

您可以使用以下方法检查代码中的授权状态

 + (CLAuthorizationStatus)authorizationStatus

编辑

kCLAuthorizationStatusNotDetermined 很可能是由位置服务被禁用引起的。您应该首先检查位置服务状态。代码应该是这样的

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
}
于 2013-03-27T11:03:35.583 回答
-14

我终于解决了!我的代表设置不正确。而不是这样做:

self.locationManager.delegate = self;

我将其更改为:

[self.locationManager setDelegate:self];

现在我的委托方法被解雇了。

感谢帮助过的人!

于 2013-03-28T16:29:23.883 回答