HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – 是否有必要使用单例CLLocationManager来避免等待设备位置更新?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_607_0@
@H_607_0@
我一次又一次地听说总是有比单身人士更好的模式,但我无法理解我的应用程序如何在不等待GPS返回数据的情况下访问设备位置(我假设该位置系统仅在显式调用时运行,如果错误纠正我).

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

解决方法

声明一个单独的类.如下所示.

@H_79_5@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;
@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]);

}
@H_607_0@

大佬总结

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

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

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:ios位置使用必要是否更新等待设备避免
猜你在找的HTML5相关文章
其他相关热搜词更多
phpJavaPython程序员load如何string使用参数jquery开发安装listlinuxiosandroid工具javascriptcap