ios – 是否有必要使用单例CLLocationManager来避免等待设备位置更新?

ios – 是否有必要使用单例CLLocationManager来避免等待设备位置更新?,第1张

概述我一次又一次地听说总是有比单身人士更好的模式,但我无法理解我的应用程序如何在不等待GPS返回数据的情况下访问设备位置(我假设该位置系统仅在显式调用时运行,如果错误则纠正我). 那么从多个(不相关的)控制器访问CLLocation数据有更好的模式吗?或者我可以期望设备位置在后台更新,即使我没有通过CLLocationManager访问它? 声明一个单独的类.如下所示. MyLocation.h @p 我一次又一次地听说总是有比单身人士更好的模式,但我无法理解我的应用程序如何在不等待GPS返回数据的情况下访问设备位置(我假设该位置系统仅在显式调用时运行,如果错误则纠正我).

那么从多个(不相关的)控制器访问CLLocation数据有更好的模式吗?或者我可以期望设备位置在后台更新,即使我没有通过CLLocationManager访问它?

解决方法 声明一个单独的类.如下所示.

MyLocation.h

@protocol MyCLControllerDelegate <NSObject>- (voID)locationUpdate:(CLLocation *)location; - (voID)locationError:(NSError *)error;@end@interface MyLocation : NSObject <CLLocationManagerDelegate> {    CLLocationManager *locationManager;    ID delegate;}@property (nonatomic,strong) CLLocationManager *locationManager;@property (nonatomic,strong) ID <MyCLControllerDelegate> delegate;

MyLocation.m

#import "MyLocation.h"@implementation MyLocation@synthesize locationManager;@synthesize delegate;- (ID) init {    self = [super init];    if (self != nil) {        if([CLLocationManager locationServicesEnabled]) {            if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenIEd || [CLLocationManager authorizationStatus] == kCLAuthorizationStatusRestricted )            {                [self showAlertWithTitle:@"Warning" anDWithMessage:@"Determining your current location cannot be performed at this time because location services are enabled but restricted"   forTargetVIEw:self];NSlog(@"Determining your current location cannot be performed at this time because location services are enabled but restricted");            }            else            {                self.locationManager = [[CLLocationManager alloc] init];                self.locationManager.delegate = self; // send loc updates to myself                [self.locationManager setDesiredAccuracy:kCLLocationAccuracyBest];                [self.locationManager setdistanceFilter:kThresholddistance];                [self.locationManager startUpdatingLocation];                NSLog(@"Location sharing set ON!");            }        } else {            [MobileYakHelper showAlertWithTitle:@"Error" anDWithMessage:@"Determining your current location cannot be performed at this time because location services are not enabled." forTargetVIEw:self];            NSLog(@"Location sharing set OFF!");        }    }    return self;}- (voID)locationManager:(CLLocationManager *)manager dIDUpdatetoLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{    NSDictionary *dictValue = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithDouble:newLocation.coordinate.latitude],@"latitude",[NSNumber numberWithDouble:newLocation.coordinate.longitude],@"longitude",nil];    [[NSUserDefaults standardUserDefaults] setValue:dictValue forKey:@"MY_LOCATION"];    CLLocationdistance meters = [newLocation distanceFromLocation:oldLocation];    if (meters >= kThresholddistance ) {        [self.delegate locationUpdate:newLocation];    }}- (voID)locationManager:(CLLocationManager *)manager       dIDFailWithError:(NSError *)error{    [self.delegate locationError:error];}@end

要在少数控制器中使用它.在它的.h文件中使用代理,并按如下方式使用:

- (voID) initializeLocations{    MyLocation _myLocation = [[MyLocation alloc] init];    _myLocation.delegate = self;    _myLocation.locationManager.desiredAccuracy = kCLLocationAccuracyBest;    CLLocation * currentLocation =  _myLocation.locationManager.location;        // Updating user's current location to server    [self sendUserCurrentCoordinate:currentLocation.coordinate];        // start updating current location    _myLocation.locationManager.desiredAccuracy = kCLLocationAccuracyBest;    [_myLocation.locationManager startUpdatingLocation];    [_myLocation.locationManager startUpdatingLocation];}- (voID)locationUpdate:(CLLocation *)location {          NSLog(@"location %@",location); }- (voID)locationError:(NSError *)error {    NSLog(@"locationdescription %@",[error description]);}
总结

以上是内存溢出为你收集整理的ios – 是否有必要使用单例CLLocationManager来避免等待设备位置更新?全部内容,希望文章能够帮你解决ios – 是否有必要使用单例CLLocationManager来避免等待设备位置更新?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1021003.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存