接口部分
typedef void (^LocationCompleteBlock)(NSDictionary* _Nullable coordinateDic,NSString* _Nullable msg);
@interface LocationService : NSObject
+ (LocationService *)shared;
- (void)getLocationWithCompletionHandler:(LocationCompleteBlock)finishBlock;
- (bool)startUpdatingLocation;
@end
实现部分
@interface LocationService ()<CLLocationManagerDelegate>
@property (nonatomic, copy) LocationCompleteBlock finishBlock;
@property (nonatomic, retain) CLLocationManager *locationManager;
@property (nonatomic, retain) NSString *msg;
@end
@implementation LocationService
static LocationService *locationService = nil;
+ (LocationService *)shared
{
@synchronized (self) {
if (locationService == nil) {
locationService = [[super alloc] init];
}
}
return locationService;
}
+ (instancetype)allocWithZone:(struct _NSZone *)zone
{
@synchronized (self) {
if (locationService == nil) {
locationService = [super allocWithZone:zone];
return locationService;
}
}
return LocationService;
}
- (id)copy
{
return self;
}
- (instancetype)init
{
self = [super init];
if (self) {
}
return self;
}
#pragma mark - 懒加载
-(CLLocationManager *)locationManager
{
if (_locationManager == nil) {
_locationManager = [[CLLocationManager alloc] init];
_locationManager.delegate = self;
}
return _locationManager;
}
#pragma mark - 外部方法
- (void)getLocationWithCompletionHandler:(LocationCompleteBlock)finishBlock
{
self.finishBlock = finishBlock;
if ([self authorizationStatus] == kCLAuthorizationStatusNotDetermined) {
[self requestWhenInUseAuthorization];
}
else if ([self authorizationStatus] == kCLAuthorizationStatusAuthorizedAlways || [self authorizationStatus] == kCLAuthorizationStatusAuthorizedWhenInUse)
{
[self startUpdatingLocation];
}
else
{
self.finishBlock(nil,@"地理位置服务不可用");
}
}
- (bool)startUpdatingLocation
{
if ([self canUseLocation]) {
[self.locationManager startUpdatingLocation];
return YES;
}
return NO;
}
#pragma mark - 内部方法
-(bool)canUseLocation
{
if ([CLLocationManager locationServicesEnabled] == NO) {
return NO;
}
if ([self authorizationStatus] != kCLAuthorizationStatusAuthorizedAlways && [self authorizationStatus] != kCLAuthorizationStatusAuthorizedWhenInUse) {
return NO;
}
return YES;
}
-(CLAuthorizationStatus)authorizationStatus
{
if (@available(iOS 14.0, *)) {
return [self.locationManager authorizationStatus];
}
else
{
return [CLLocationManager authorizationStatus];
}
}
-(void)requestWhenInUseAuthorization
{
if ([CLLocationManager locationServicesEnabled]) {
[self.locationManager requestWhenInUseAuthorization];
}
}
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations
{
CLLocation *location = locations.lastObject;
if (self.finishBlock) {
if (location.horizontalAccuracy < 0) {
self.finishBlock(nil,@"获取定位失败,请重新尝试");
}
else
{
NSMutableDictionary *mutableDic = [NSMutableDictionary dictionary];
mutableDic[@"latitude"] = [NSNumber numberWithDouble:location.coordinate.latitude];
mutableDic[@"longitude"] = [NSNumber numberWithDouble:location.coordinate.longitude];
self.finishBlock(mutableDic,self.msg);
}
}
[self.locationManager stopUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
if (self.finishBlock) {
self.finishBlock(nil,error ? [error description] : nil);
}
[self.locationManager stopUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
switch (status) {
case kCLAuthorizationStatusNotDetermined:
{
//定位开启,未决定进入此状态
break;
}
case kCLAuthorizationStatusRestricted:
{
//访问受限
break;
}
case kCLAuthorizationStatusDenied:
{
//用户拒绝或定位关闭,进入此状态
if([CLLocationManager locationServicesEnabled])
{
//定位可用,说明用户拒绝了授权
}
else
{
//否则为定位关闭
}
break;
}
case kCLAuthorizationStatusAuthorizedAlways:
{
//前后台授权
break;
}
case kCLAuthorizationStatusAuthorizedWhenInUse:
{
//前台授权
break;
}
default:
break;
}
}
@end