gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 02:11:19 24 4
gpt4 key购买 nike

我正在创建一个使用地理围栏的 map 应用程序。我让地理定位工作在 2 个 View Controller 上“根据需要”实例化位置管理器,但这是糟糕的做法,所以我创建了一个单例 LocationManager类(class)。

当我访问currentLocation时来 self 的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之前,但调整它以引用单例 LocationManagerMapViewController生成此日志:

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;
}

最佳答案

在我的 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)

关于ios - 在单例 LocationManager 类中提示位置授权的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29257769/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com