iOS定位工具類--block傳值

在iOS開發(fā)中,經(jīng)常會碰到定位到當(dāng)前城市的需求,系統(tǒng)自帶的定位功能就能實現(xiàn),于是封裝了一個類方便使用,整理一下發(fā)出來共勉,大家多提意見,多多交流??

使用

可實現(xiàn)定位到當(dāng)前地址的功能,調(diào)用簡單,block傳值

 [[MSLocationTool sharedMSLocationTool] getCurrentLOcation:^(CLLocation *location, CLPlacemark *pl, NSString *error) {
         if ([error length] > 0) {
             NSLog(@"定位有錯誤-->%@",error);   
         }else{
             [self.locationBtn setTitle:pl.locality forState:UIControlStateNormal];
                
             [[NSUserDefaults standardUserDefaults] setValue:pl.locality forKey:CITY_KEY];
             [[NSUserDefaults standardUserDefaults] synchronize];
                
         }
            
}];

實現(xiàn)

  • 第一步:請求授權(quán)

在Info.plist中配置相關(guān)key:
Privacy - Location When In Use Usage Description
Privacy - Location Always Usage Description

//后面為字符串類型(隨意填寫) 例如:請您允許,我們將會為您提供更精確的信息
Privacy - Location When In Use Usage Description   
//后面為字符串類型(隨意填寫)例如:請您允許,我們將會為您提供更精確的信息 
Privacy - Location Always Usage Description        
// 特別聲明一下,這個字段的添加要看你的需求,這個功能是指在后臺時也可以時時定位,
所以app如果不需要的話,不建議添加,否則上線審核會出現(xiàn)問題,
如果需要這個功能,要做一些聲明描述:GPS在后臺持續(xù)運行,可以大大降低電池的壽命。
info.plist配置.png
  • 第二步:單例宏

創(chuàng)建一個Header File來實現(xiàn)單例宏,Singleton.h內(nèi)容如下:

Singleton.png
#ifndef Singleton_h
#define Singleton_h

/**
 *  在.h文件中定義的宏,arc
 *
 *  MSSingletonH(name) 這個是宏
 *  + (instancetype)shared##name;這個是被代替的方法, ##代表著shared+name 高度定制化
 * 在外邊我們使用 “MSSingletonH(gege)” 那么在.h文件中,定義了一個方法"+ (instancetype)sharedgege",所以,第一個字母要大寫
 *
 *  @return 一個搞定好的方法名
 */
#define MSSingletonH(name) + (instancetype)shared##name;


/**
 *  在.m文件中處理好的宏 arc
 *
 *  MSSingletonM(name) 這個是宏,因為是多行的東西,所以每行后面都有一個"\",最后一行除外,
 * 之所以還要傳遞一個“name”,是因為有個方法要命名"+ (instancetype)shared##name"
 *  @return 單利
 */
#define MSSingletonM(name) \
static id instance_ = nil;\
+ (instancetype)shared##name{\
static dispatch_once_t onceToken;\
dispatch_once(&onceToken, ^{\
instance_ = [[self alloc] init];\
});\
return instance_;\
}\
+ (instancetype)allocWithZone:(struct _NSZone *)zone{\
static dispatch_once_t onceToken;\
dispatch_once(&onceToken, ^{\
instance_ = [super allocWithZone:zone];\
});\
return instance_;\
}\
- (id)copyWithZone:(NSZone *)zone{\
return instance_;\
}

#endif /* Singleton_h */


  • 第三步:繼承自NSObject的工具類MSLocationTool

工具類繼承自NSObject。

MSLocationTool.h文件內(nèi)容如下:
1、導(dǎo)入系統(tǒng)自帶的庫#import <CoreLocation/CoreLocation.h>;
2、單例;
3、block實現(xiàn)返回值

#import <Foundation/Foundation.h>
#import "Singleton.h"
#import <CoreLocation/CoreLocation.h>


typedef void(^ResultCityBlock)(CLLocation *location,CLPlacemark *pl,NSString *error);


@interface MSLocationTool : NSObject


MSSingletonH(MSLocationTool)

-(void)getCurrentLOcation:(ResultCityBlock)block;


@end

MSLocationTool.m文件內(nèi)容如下:

1、導(dǎo)入頭文件,屬性聲明
2、方法實現(xiàn)

#import "MSLocationTool.h"
#import <UIKit/UIKit.h>
#import "Singleton.h"

#define isIOS(version) ([[UIDevice currentDevice].systemVersion floatValue] >= version)

@interface MSLocationTool()<CLLocationManagerDelegate>

@property(nonatomic,copy)ResultCityBlock block;

/** manager */
@property(nonatomic,strong)CLLocationManager *locationManager;

/** 地理編碼 */
@property(nonatomic,strong)CLGeocoder *geoC;


@end


@implementation MSLocationTool

//單例
MSSingletonM(MSLocationTool)

#pragma mark - 懶加載
-(CLLocationManager *)locationManager
{
    if (_locationManager == nil) {
        _locationManager = [[CLLocationManager alloc] init];
        _locationManager.delegate = self;
        
        //獲取info.plist里的鍵值對
        NSDictionary *infoDict = [NSBundle mainBundle].infoDictionary;
        
        
        if (isIOS(8.0)) {
            
            //獲取后臺定位描述
            NSString *alwaysStr = infoDict[@"NSLocationAlwaysUsageDescription"];
            NSString *whenUserStr = infoDict[@"NSLocationWhenInUseUsageDescription"];
            
            // 根據(jù)開發(fā)者的設(shè)置 請求定位授權(quán)
            if ([alwaysStr length] > 0) {
                [_locationManager requestAlwaysAuthorization];
                
            }else if ([whenUserStr length] > 0){
                [_locationManager requestWhenInUseAuthorization];
                
                NSArray *backModels = infoDict[@"UIBackgroundModes"];
                if (![backModels containsObject:@"location"]) {
                    //前臺定位授權(quán),如果想在后臺獲取位置,需勾選后臺模式location update
                    
                }else{
                    if (isIOS(9.0)) {
                        _locationManager.allowsBackgroundLocationUpdates = YES;
                    }
                    
                }
                
                
            }
     
            
        }else{
            // 如果請求的是前臺定位授權(quán), 如果想要在后臺獲取用戶位置, 提醒其他開發(fā)者, 勾選后臺模式location updates
            NSArray *backModes = infoDict[@"UIBackgroundModes"];
            if (![backModes containsObject:@"location"]) {
                NSLog(@"當(dāng)前授權(quán)模式, 如果想要在后臺獲取位置, 需要勾選后臺模式location updates");
            }
            
            
        }
        
        
    }
    
    return _locationManager;
}

-(CLGeocoder *)geoC
{
    if (_geoC == nil) {
        _geoC = [[CLGeocoder alloc] init];
    }
    return _geoC;
}

#pragma mark - 方法
-(void)getCurrentLOcation:(ResultCityBlock)block
{
    //記錄代碼塊
    self.block = block;
    
    //獲取位置信息
    if ([CLLocationManager locationServicesEnabled]) {
        
        [self.locationManager startUpdatingLocation];
        
    }else{
        self.block(nil, nil, @"定位服務(wù)未開啟");
        
    }
}

#pragma mark - 代理方法
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations
{
    CLLocation *location = [locations lastObject];
    
    //判斷位置是否可用
    if (location.horizontalAccuracy >= 0) {
        [self.geoC reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
           
            if (error == nil) {
                CLPlacemark *pl = [placemarks firstObject];
                self.block(location, pl, 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;
        }
         
        //定位關(guān)閉 或 對此APP授權(quán)未never時調(diào)用
        case kCLAuthorizationStatusDenied:
        {
            if ([CLLocationManager locationServicesEnabled]) {
                NSLog(@"定位開啟,但被拒");
                self.block(nil, nil, @"被拒絕");
                
            }else{
                NSLog(@"定位關(guān)閉,不可用");
                self.block(nil, nil, @"定位關(guān)閉,不可用");
            }
            
            break;
        }
            
        case kCLAuthorizationStatusAuthorizedAlways:
            NSLog(@"獲取前后臺定位授權(quán)");
            break;
            
        case kCLAuthorizationStatusAuthorizedWhenInUse:
            NSLog(@"獲取前臺定位授權(quán)");
            break;
            
            
            
        default:
            break;
    }
}

@end

參考鏈接
代碼附上--GitHub鏈接

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容