简体   繁体   English

CLLocationManager仅在第一次使用

[英]CLLocationManager only works first time

I wrote a wrapper around CLLocationManager class. 我围绕CLLocationManager类编写了一个包装器。 There are different UIViewControllers using the LocationManager class. 使用LocationManager类的UIViewControllers不同。 It's a singleton and I set it to nil after use. 这是一个单例,使用后我将其设置为nil。 However, it only works with the first class that uses it. 但是,它仅适用于使用它的第一类。 How to kill it? 如何杀死它? I'm already calling stopUpdatingLocation . 我已经在调用stopUpdatingLocation

I've tried this already 我已经尝试过

this post suggests that I shouldn't use a singleton, but if I do that, it doesn't work at all. 这篇文章表明我不应该使用单例,但是如果我这样做,那根本就行不通。

MyViewController.m MyViewController.m

LocationManager * locationManager;

@implementation MyViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    locationManager = [LocationManager sharedManager];
    [locationManager start];
    locationManager.delegate = self;
}

#pragma mark - Delegate functions

-(void)isLocationManagerDelegateTriggered:(CLLocation *) location {
    // works only first time, but gets triggered every time    
    [locationManager stop];
    locationManager.delegate = nil;
    NSLog(@"latitude = %f longitude = %f",location.coordinate.latitude, location.coordinate.longitude);
}

LocationManager.m LocationManager.m

#import <CoreLocation/CoreLocation.h>

@implementation LocationManager

CLLocationManager *clLocationManager;

@synthesize delegate;

/* Singleton */
static LocationManager *sharedSingleton;

/* Singleton */
+ (id)sharedManager
{
    static LocationManager *sharedMyManager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedMyManager = [[self alloc] init];
        clLocationManager = [[CLLocationManager alloc] init];
    });
    return sharedMyManager;
}

#pragma mark - Public functions

- (void)start {

    [clLocationManager setDelegate:(id<CLLocationManagerDelegate>)self]; //If I put this         [clLocationManager setDesiredAccuracy:kCLLocationAccuracyBest]; // and this in initialize, it doesn't work anymore.

    [clLocationManager startUpdatingLocation];
}

- (void)stop {
    [clLocationManager stopUpdatingLocation];
}

#pragma mark - CLLocationManagerDelegate

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
        CLLocation *currentLocation = newLocation;

        if (currentLocation != nil) {
            [delegate isLocationManagerDelegateTriggered:currentLocation];
        }
}

@end

LocationManager.h LocationManager.h

#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>

@class LocationManager;

@protocol LocationManagerDelegate

@required
-(void)isLocationManagerDelegateTriggered:(CLLocation *) location;
@end

@interface LocationManager : NSObject <CLLocationManagerDelegate>

+ (id)sharedManager;
-(void)start;
- (void)stop;
- (double) getDistanceToLocationInKM:(CLLocation *) location currentLocation:(CLLocation *)currentlocation;

@property (nonatomic, assign) id  delegate;

@end

The delegate gets fired only once: 代表仅被解雇一次:

2014-05-17 20:04:03.110 Wobbly[2064:60b] latitude = 50.865281 longitude = 5.732814 2014-05-17 20:04:03.110 Wobbly [2064:60b]纬度= 50.865281经度= 5.732814

You are allocating the object using: 您正在使用以下方法分配对象:

locationManager = [[LocationManager alloc]init];

It won't create a singleton instance. 它不会创建单例实例。

Instead of that you need to use like: 而不是那样,您需要使用:

I wrote a wrapper around CLLocationManager class. 我围绕CLLocationManager类编写了一个包装器。 There are different UIViewControllers using the LocationManager class. 使用LocationManager类的UIViewControllers不同。 It's a singleton and I set it to nil after use. 这是一个单例,使用后我将其设置为nil。 However, it only works with the first class that uses it. 但是,它仅适用于使用它的第一类。 How to kill it? 如何杀死它? I'm already calling stopUpdatingLocation. 我已经在打电话给stopUpdatingLocation。

I've tried this already 我已经尝试过了

this post suggests that I shouldn't use a singleton, but if I do that, it doesn't work at all. 这篇文章表明我不应该使用单例,但是如果我这样做,那根本就行不通。

MyViewController.m MyViewController.m

LocationManager * locationManager;

@implementation MyViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    locationManager = [LocationManager sharedManager];
    [locationManager start];
    locationManager.delegate = self;
}

#pragma mark - Delegate functions

-(void)isLocationManagerDelegateTriggered:(CLLocation *) location
{
    // works only first time, but gets triggered every time    
    [locationManager stop];
    locationManager.delegate = nil;
}

LocationManager.m LocationManager.m

#import <CoreLocation/CoreLocation.h>

@implementation LocationManager

CLLocationManager *clLocationManager;

@synthesize delegate;

/* Singleton */
+ (id)sharedManager
{
    static LocationManager *sharedMyManager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedMyManager = [[self alloc] init];
        clLocationManager = [[CLLocationManager alloc] init];
    });
    return sharedMyManager;
}

#pragma mark - Public functions

- (void)start
{

    [clLocationManager setDelegate:(id<CLLocationManagerDelegate>)self]; //If I put this         [clLocationManager setDesiredAccuracy:kCLLocationAccuracyBest]; // and this in initialize, it doesn't work anymore.

    [clLocationManager startUpdatingLocation];
}

- (void)stop
{
    [clLocationManager stopUpdatingLocation];
}

#pragma mark - CLLocationManagerDelegate

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    if(firsttime)
    {
        firsttime = false;
        CLLocation *currentLocation = newLocation;

        if (currentLocation != nil)
        {
            [delegate isLocationManagerDelegateTriggered:currentLocation];
        }
    }
}

@end

LocationManager.h LocationManager.h

#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>

@class LocationManager;

@protocol LocationManagerDelegate

@required
-(void)isLocationManagerDelegateTriggered:(CLLocation *) location;
@end

@interface LocationManager : NSObject <CLLocationManagerDelegate>

+ (id)sharedManager
- (void)start;
- (void)stop;
- (double) getDistanceToLocationInKM:(CLLocation *) location currentLocation:(CLLocation *)currentlocation;

@property (nonatomic, assign) id  delegate;

@end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 CLLocationManager仅在安装后第一次运行应用程序时监视区域 - CLLocationManager only monitors regions the first time the app is run after install addsubview动画只能在第一次使用 - addsubview animation works only first time UICollisionBehavior仅在第一次添加后才能工作 - UICollisionBehavior only works after adding the first time Watchkit-didReceiveApplicationContext仅在第一次工作 - Watchkit - didReceiveApplicationContext works only for the first time writeToFile:atomically仅在第一次使用 - writeToFile:atomically only works first time NSURLSession dataTaskWithRequest 仅适用于第一次 - NSURLSession dataTaskWithRequest only works first time APP 只能在 ios 上第一次运行 - APP only works the first time on ios CATransition的ios代码只能在第一次使用? - ios code for CATransition works only the first time? 首先显示UIAlertController,然后显示MFMailComposeViewController。 仅在第一次工作 - Showing UIAlertController first then MFMailComposeViewController. Works only first time 首次安装应用程序时,CLLocationManager返回位置为0.00 - CLLocationManager returns location as 0.00 when app is installed for the first time
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM