我需要在项目中使用位置服务,但是locationManager: didupdateLocations:
确实调用过。
1.我已将
Privacy - Location When In Use Usage Description
添加到info.plist 我正在测试我的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,但它也没有用。