#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
#import "Single.h"
typedef void(^ResultBlock)(CLLocation *location, CLPlacemark *placemark, NSString *error);
@interface LSLLocationTool : NSObject
SingleInterface(LSLLocationTool);
- (void)getCurrentLocation:(ResultBlock)block;
@end
#import "LSLLocationTool.h"
#import <UIKit/UIKit.h>
#define isIOS(version) ([[UIDevice currentDevice].systemVersion floatValue] >= version)
@interface LSLLocationTool()<CLLocationManagerDelegate>
@property(nonatomic,copy)ResultBlock block;
@property(nonatomic,strong)CLLocationManager *locationManager;
@property(nonatomic,strong)CLGeocoder *geocoder;
@end
@implementation LSLLocationTool
SingleImplement(LSLLocationTool);
#pragma mark - 懒加载
- (CLLocationManager *)locationManager
{
if (!_locationManager) {
_locationManager = [[CLLocationManager alloc] init];
_locationManager.delegate = self;
NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary];
if (isIOS(8.0)) {
NSString *alwaysStr = infoDict[@"NSLocationAlwaysUsageDescription"];
NSString *whenInUseStr = infoDict[@"NSLocationWhenInUseUsageDescription"];
if([alwaysStr length] > 0){
[_locationManager requestAlwaysAuthorization];
}else if ([whenInUseStr length] > 0){
[_locationManager requestWhenInUseAuthorization];
NSArray *backModes = infoDict[@"UIBackgroundModes"];
if (![backModes containsObject:@"location"]) {
NSLog(@"当前授权模式是前台定位授权, 如果想要在后台获取位置, 需要勾选后台模式location updates");
}else {
if(isIOS(9.0)){
_locationManager.allowsBackgroundLocationUpdates = YES;
}
}
}else{
NSLog(@"如果在iOS8.0之后获取用户位置, 必须主动填写info.plist文件中的key NSLocationAlwaysUsageDescription 或者 NSLocationWhenInUseUsageDescription");
}
}else{
NSArray *backModes = infoDict[@"UIBackgroundModes"];
if (![backModes containsObject:@"location"]) {
NSLog(@"当前授权模式, 如果想要在后台获取位置, 需要勾选后台模式location updates");
}
}
}
return _locationManager;
}
- (CLGeocoder *)geocoder
{
if (!_geocoder) {
_geocoder = [[CLGeocoder alloc] init];
}
return _geocoder;
}
#pragma mark - 反地理编码接口的实现
- (void)getCurrentLocation:(ResultBlock)block
{
self.block = block;
if ([CLLocationManager locationServicesEnabled] ) {
[self.locationManager startUpdatingLocation];
}else{
self.block(nil,nil,@"用户未开启定位服务");
}
}
#pragma mark - <CLLocationManagerDelegate> 实现代理方法
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations
{
CLLocation *location = [locations firstObject];
if (location.horizontalAccuracy >= 0) {
[self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
if (error == nil) {
CLPlacemark *placemark = [placemarks firstObject];
self.block(location,placemark,nil);
}else{
self.block(location,nil,@"反编码失败");
}
}];
}
[manager stopUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
switch (status) {
case kCLAuthorizationStatusNotDetermined:
{
NSLog(@"用户还未决定");
break;
}
case kCLAuthorizationStatusRestricted:
{
NSLog(@"访问受限");
self.block(nil, nil, @"访问受限");
break;
}
case kCLAuthorizationStatusDenied:
{
if([CLLocationManager locationServicesEnabled])
{
NSLog(@"定位开启,但被拒");
self.block(nil, nil, @"被拒绝");
}else
{
NSLog(@"定位关闭,不可用");
self.block(nil, nil, @"定位关闭,不可用");
}
break;
}
case kCLAuthorizationStatusAuthorizedAlways:
{
NSLog(@"获取前后台定位授权");
break;
}
case kCLAuthorizationStatusAuthorizedWhenInUse:
{
NSLog(@"获得前台定位授权");
break;
}
default:
break;
}
}
@end
使用示例:
#import "ViewController.h"
#import "LSLLocationTool.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[[LSLLocationTool shareLSLLocationTool] getCurrentLocation:^(CLLocation *location, CLPlacemark *placemark, NSString *error) {
if([error length] > 0){
NSLog(@"报错--%@", error);
}else{
NSLog(@"%@", placemark.name);
}
}];
}
@end