在单例 LocationManager 中提示位置授权的位置 class
Where to prompt for location authorization in singleton LocationManager class
我正在创建一个使用地理围栏的地图应用程序。我让地理定位工作在 2 个视图控制器上实例化位置管理器 "as needed",但这是糟糕的做法,所以我创建了一个单例 LocationManager
class。
当我使用以下代码从我的 MapViewController
访问 currentLocation
时:
[LocationManager sharedInstance].currentLocation;
我收到此警告:
Property access result unused - getters should not be used for side effects
更新:立即解决问题,创建新问题
在我的 MapViewController.m
中,我在创建单例之前声明了这个 属性 LocationManager
class:
@property (nonatomic, strong) CLLocation *currentLocation;
我用这一行将 属性 设置为单例中的值:
self.currentLocation = [LocationManager sharedInstance].currentLocation;
...警告消失了。
新问题:在哪里请求位置授权
解决了这个基本问题后,我遇到了一个新问题:
我需要此代码才能进入我的 LocationManager
class:
if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
[self.locationManager requestAlwaysAuthorization];
}
我在 startUpdatingLocation
之前在我的 MapViewController
中的 viewWillAppear
中有它,但是调整它以引用 MapViewController
中的单例 LocationManager
生成这个日志:
Trying to start MapKit location updates without prompting for location authorization. Must call -[CLLocationManager requestWhenInUseAuthorization] or -[CLLocationManager requestAlwaysAuthorization] first.
我想我需要在我的 LocationManager
class 中放置位置授权提示,但是 where 把我绊倒了。
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;
}
在我的 MapViewController.m
中,我在创建单例之前声明了这个 属性 LocationManager
class:
@property (nonatomic, strong) CLLocation *currentLocation;
我用这一行将 属性 设置为单例中的值:
self.currentLocation = [LocationManager sharedInstance].currentLocation;
...警告消失了。
虽然我很想删除这个 post,但我还是保留了它,以防其他人在学习教程后遇到同样的问题。
更新:第二个问题已解决:
- (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
class 的初始化程序中设置了断点并发现:
if (!self)
应该是:
if (!self.locationManager)
我正在创建一个使用地理围栏的地图应用程序。我让地理定位工作在 2 个视图控制器上实例化位置管理器 "as needed",但这是糟糕的做法,所以我创建了一个单例 LocationManager
class。
当我使用以下代码从我的 MapViewController
访问 currentLocation
时:
[LocationManager sharedInstance].currentLocation;
我收到此警告:
Property access result unused - getters should not be used for side effects
更新:立即解决问题,创建新问题
在我的 MapViewController.m
中,我在创建单例之前声明了这个 属性 LocationManager
class:
@property (nonatomic, strong) CLLocation *currentLocation;
我用这一行将 属性 设置为单例中的值:
self.currentLocation = [LocationManager sharedInstance].currentLocation;
...警告消失了。
新问题:在哪里请求位置授权
解决了这个基本问题后,我遇到了一个新问题:
我需要此代码才能进入我的 LocationManager
class:
if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
[self.locationManager requestAlwaysAuthorization];
}
我在 startUpdatingLocation
之前在我的 MapViewController
中的 viewWillAppear
中有它,但是调整它以引用 MapViewController
中的单例 LocationManager
生成这个日志:
Trying to start MapKit location updates without prompting for location authorization. Must call -[CLLocationManager requestWhenInUseAuthorization] or -[CLLocationManager requestAlwaysAuthorization] first.
我想我需要在我的 LocationManager
class 中放置位置授权提示,但是 where 把我绊倒了。
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;
}
在我的 MapViewController.m
中,我在创建单例之前声明了这个 属性 LocationManager
class:
@property (nonatomic, strong) CLLocation *currentLocation;
我用这一行将 属性 设置为单例中的值:
self.currentLocation = [LocationManager sharedInstance].currentLocation;
...警告消失了。
虽然我很想删除这个 post,但我还是保留了它,以防其他人在学习教程后遇到同样的问题。
更新:第二个问题已解决:
- (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
class 的初始化程序中设置了断点并发现:
if (!self)
应该是:
if (!self.locationManager)