use*_*ser 5 cocoa-touch design-patterns cllocationmanager ios
我一次又一次听到,总是有比单例更好的模式,但是我不明白我的应用程序还能如何在不等待GPS返回数据的情况下访问设备位置(我假设该位置系统仅在明确要求时运行,如果出错请更正我)。
那么,是否存在更好的模式来从多个(不相关的)控制器访问CLLocation数据?还是即使我不通过CLLocationManager访问设备位置,也可以期望设备位置在后台更新?
声明一个类。就像下面这样。
我的位置.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;
Run Code Online (Sandbox Code Playgroud)
我的位置.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
Run Code Online (Sandbox Code Playgroud)
要在少数控制器中使用它,请在其 .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]);
}
Run Code Online (Sandbox Code Playgroud)