0

我需要在我的项目中使用位置服务,但locationManager: didupdateLocations:确实调用了。

1.我已经添加Privacy - Location When In Use Usage Description到info.plist。
2.我正在我的测试 iPhone6 设备上进行测试。

这是我的代码:

@interface VCLocationManager ()<CLLocationManagerDelegate>

@property (nonatomic, strong) CLLocationManager *locManager;

@property (nonatomic, copy) VCLocationBlock block;

@end

@implementation VCLocationManager

+ (instancetype)sharedManager {

    static VCLocationManager *_manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
         _manager = [[VCLocationManager alloc] init];
    });

     return _manager;
}

- (instancetype)init {

    self = [super init];
    if (self) {

        _locManager = [[CLLocationManager alloc] init];
        [_locManager setDesiredAccuracy:(kCLLocationAccuracyBest)];
        _locManager.distanceFilter = 100;
        _locManager.delegate = self;

        if (![CLLocationManager locationServicesEnabled]) {
            NSLog(@"Open location service.");
        } else {

            CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
            if (status == kCLAuthorizationStatusNotDetermined) {
                [_locManager requestWhenInUseAuthorization];
            }
        }

        [_locManager startUpdatingLocation];
    }
    return self;
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {

    CLLocationCoordinate2D coor = locations.lastObject.coordinate;

    NSString *lat = [NSString stringWithFormat:@"%@",@(coor.latitude)];
    NSString *lon = [NSString stringWithFormat:@"%@",@(coor.longitude)];

    [VCLocationManager sharedManager].lat = lat;
    [VCLocationManager sharedManager].lon = lon;

    self.block(lat, lon);

    [self.locManager stopUpdatingLocation];
}

我在行设置了一个断点[_locManager startUpdatingLocation];,但是 locationManager: didupdataLocations: 没有被调用,我试图在主线程上初始化 _locManager 但它也没有工作。

4

0 回答 0