在单例LocationManager类中提示位置授权的位置

时间:2015-03-25 13:57:07

标签: ios objective-c geolocation cllocationmanager

我正在创建一个使用地理围栏的地图应用。我得到地理位置工作,实例化一个位置经理"根据需要"在2个视图控制器上,但这是一个糟糕的做法,所以我创建了一个单独的LocationManager类。

当我使用以下代码从currentLocation访问MapViewController时:

[LocationManager sharedInstance].currentLocation;

我收到了这个警告:

Property access result unused - getters should not be used for side effects

更新:问题立即解决,新问题已解决

在我的MapViewController.m中,我在创建单个LocationManager类之前声明了此属性:

@property (nonatomic, strong) CLLocation *currentLocation;

我用该行将该属性设置为单例中的值:

self.currentLocation = [LocationManager sharedInstance].currentLocation;

......警告消失了。

新问题:请求位置授权的位置

在解决了这个基本问题后,我遇到了一个新问题:

我需要将此代码放在LocationManager类中的某个位置:

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

我之前在viewWillAppear之前的MapViewController中已在startUpdatingLocation中找到了它,但是调整它以引用LocationManager中的单身MapViewController会生成此日志:

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

我想我需要在我的LocationManager课程中提示位置授权,但是在哪里绊倒我。

LocationManager.h

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

@interface LocationManager : NSObject <CLLocationManagerDelegate>

+(LocationManager *)sharedInstance;

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

- (void)startUpdatingLocation;

LocationManager.m

#import "LocationManager.h"

@implementation LocationManager

+ (LocationManager *)sharedInstance {
    static LocationManager *instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^ {
        instance = [[self alloc]init];
    });
    return instance;
}

- (id)init {
    self = [super init];
    if (!self) {
        self.locationManager = [[CLLocationManager alloc]init];
        self.locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
        self.locationManager.distanceFilter = 100; // meters
        self.locationManager.delegate = self;
    }
    return self;
}

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

    [self.locationManager startUpdatingLocation];
}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    NSLog(@"Location service failed with error %@", error);
}

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
    CLLocation *location = [locations lastObject];
    NSLog(@"Latitude %+.6f, Longitude %+.6f\n",
          location.coordinate.latitude,
          location.coordinate.longitude);
    self.currentLocation = location;
}

1 个答案:

答案 0 :(得分:1)

在我的MapViewController.m中,我在创建单个LocationManager类之前声明了此属性:

@property (nonatomic, strong) CLLocation *currentLocation;

我用该行将该属性设置为单例中的值:

self.currentLocation = [LocationManager sharedInstance].currentLocation;

......警告消失了。

虽然我很想删除这篇文章,但是我会把它留下来以防其他人在教程后遇到同样的问题。

更新:解决了第二个问题:

- (id)init {
        self = [super init];
    if (!self.locationManager) {
        self.locationManager = [[CLLocationManager alloc]init];

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

        self.locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
        self.locationManager.distanceFilter = 100; // meters
        self.locationManager.delegate = self;
    }
    return self;
}

我在我的LocationManager课程中向初始化程序中添加了一个断点并发现:

if (!self)

应该是:

if (!self.locationManager)