大佬教程收集整理的这篇文章主要介绍了ios – 是否有必要使用单例CLLocationManager来避免等待设备位置更新?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
那么从多个(不相关的)控制器访问CLLOCATIOn数据有更好的模式吗?或者我可以期望设备位置在后台更新,即使我没有通过CLLOCATIOnManager访问它?
@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;@H_79_5@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来避免等待设备位置更新?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。