[MAMapKit]
要在iOS 11及以上版本使用定位服务, 需要在Info.plist
中添加NSLocationAlwaysAndWhenInUseUsageDescription
和NSLocationWhenInUseUsageDescription
字段。
⚠️注意循环引用
LocalPrivateObj.h
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
typedef void (^OperationBlock)(void);
typedef void (^FailedBlock)(NSString *tip);
@interface LocalPrivateObj : NSObject
///位置授权
- (void)extLocalPermissions:(OperationBlock)authedBlock failed:(FailedBlock)failedBlock deniedBlock:(OperationBlock)deniedBlock;
@end
LocalPrivateObj.m
#import "LocalPrivateObj.h"
@interface LocalPrivateObj ()<CLLocationManagerDelegate>
@property (nonatomic, strong) CLLocationManager *locationMgr;
@property (nonatomic, copy) OperationBlock authBlock;
@property (nonatomic, copy) OperationBlock deniedBlock;
@end
@implementation LocalPrivateObj
- (instancetype)init {
self = [super init];
if (self) {
self.locationMgr = [[CLLocationManager alloc] init];
self.locationMgr.delegate = self;
}
return self;
}
///位置授权
- (void)extLocalPermissions:(OperationBlock)authedBlock failed:(FailedBlock)failedBlock deniedBlock:(OperationBlock)deniedBlock {
//关心定位权限问题:设备根本就不支持?用户是否授权?
if (![CLLocationManager locationServicesEnabled]) {
if (failedBlock) {
failedBlock(@"设备不支持定位!");
}
return;
}
CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
switch (status) {
case kCLAuthorizationStatusAuthorizedAlways:
case kCLAuthorizationStatusAuthorizedWhenInUse:
NSLog(@"可用定位");
if (authedBlock) {
authedBlock();
}
break;
case kCLAuthorizationStatusNotDetermined: {
self.authBlock = authedBlock;
self.deniedBlock = deniedBlock;
NSLog(@"重新请求定位");
[self.locationMgr requestWhenInUseAuthorization];
}
break;
case kCLAuthorizationStatusRestricted:
case kCLAuthorizationStatusDenied: {
if (deniedBlock) {
deniedBlock();
}
NSLog(@"拒绝❌定位");
}
break;
default:
break;
}
}
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
[self locationAuth:status];
}
- (void)locationManagerDidChangeAuthorization:(CLLocationManager *)manager API_AVAILABLE(ios(14.0)) {
CLAuthorizationStatus status = manager.authorizationStatus;
[self locationAuth:status];
}
- (void)locationAuth:(CLAuthorizationStatus)status {
switch (status) {
case kCLAuthorizationStatusAuthorizedAlways:
case kCLAuthorizationStatusAuthorizedWhenInUse:
if (self.authBlock) {
self.authBlock();
}
NSLog(@"可用定位");
break;
case kCLAuthorizationStatusNotDetermined: {
NSLog(@"重新请求定位");
[self.locationMgr requestWhenInUseAuthorization];
}
break;
case kCLAuthorizationStatusRestricted:
case kCLAuthorizationStatusDenied: {
if (self.deniedBlock) {
self.deniedBlock();
}
NSLog(@"拒绝❌定位");
}
break;
default:
break;
}
}
@end
Demo
//授权
__weak __typeof(self)weakSelf = self;
self.localObj = [[LocalPrivateObj alloc] init];
[self.localObj extLocalPermissions:^{
[weakSelf locationOnce];
} failed:^(NSString *str) {
[SVProgressHUD showErrorWithStatus:str];
} deniedBlock:^{
NSLog(@"拒绝❌定位");
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"app未授权定位" preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}]];
[alert addAction:[UIAlertAction actionWithTitle:@"去设置" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
if (@available(iOS 10.0, *)) {
[UIApplication.sharedApplication openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString] options:@{} completionHandler:nil];
} else {
[UIApplication.sharedApplication openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
}
}]];
[weakSelf presentViewController:alert animated:YES completion:nil];
}];
网友评论