在iOS開發(fā)中嵌洼,經(jīng)常會(huì)碰到定位到當(dāng)前城市的需求,系統(tǒng)自帶的定位功能就能實(shí)現(xiàn)祝沸,于是封裝了一個(gè)類方便使用矮烹,整理一下發(fā)出來共勉,大家多提意見罩锐,多多交流??
使用
可實(shí)現(xiàn)定位到當(dāng)前地址的功能奉狈,調(diào)用簡單,block傳值
[[MSLocationTool sharedMSLocationTool] getCurrentLOcation:^(CLLocation *location, CLPlacemark *pl, NSString *error) {
if ([error length] > 0) {
NSLog(@"定位有錯(cuò)誤-->%@",error);
}else{
[self.locationBtn setTitle:pl.locality forState:UIControlStateNormal];
[[NSUserDefaults standardUserDefaults] setValue:pl.locality forKey:CITY_KEY];
[[NSUserDefaults standardUserDefaults] synchronize];
}
}];
實(shí)現(xiàn)
-
第一步:請求授權(quán)
在Info.plist中配置相關(guān)key:
Privacy - Location When In Use Usage Description
Privacy - Location Always Usage Description
//后面為字符串類型(隨意填寫) 例如:請您允許涩惑,我們將會(huì)為您提供更精確的信息
Privacy - Location When In Use Usage Description
//后面為字符串類型(隨意填寫)例如:請您允許仁期,我們將會(huì)為您提供更精確的信息
Privacy - Location Always Usage Description
// 特別聲明一下,這個(gè)字段的添加要看你的需求竭恬,這個(gè)功能是指在后臺時(shí)也可以時(shí)時(shí)定位跛蛋,
所以app如果不需要的話,不建議添加痊硕,否則上線審核會(huì)出現(xiàn)問題赊级,
如果需要這個(gè)功能,要做一些聲明描述:GPS在后臺持續(xù)運(yùn)行岔绸,可以大大降低電池的壽命理逊。
-
第二步:單例宏
創(chuàng)建一個(gè)Header File來實(shí)現(xiàn)單例宏橡伞,Singleton.h內(nèi)容如下:
#ifndef Singleton_h
#define Singleton_h
/**
* 在.h文件中定義的宏,arc
*
* MSSingletonH(name) 這個(gè)是宏
* + (instancetype)shared##name;這個(gè)是被代替的方法晋被, ##代表著shared+name 高度定制化
* 在外邊我們使用 “MSSingletonH(gege)” 那么在.h文件中骑歹,定義了一個(gè)方法"+ (instancetype)sharedgege",所以,第一個(gè)字母要大寫
*
* @return 一個(gè)搞定好的方法名
*/
#define MSSingletonH(name) + (instancetype)shared##name;
/**
* 在.m文件中處理好的宏 arc
*
* MSSingletonM(name) 這個(gè)是宏,因?yàn)槭嵌嘈械臇|西墨微,所以每行后面都有一個(gè)"\",最后一行除外道媚,
* 之所以還要傳遞一個(gè)“name”,是因?yàn)橛袀€(gè)方法要命名"+ (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實(shí)現(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薄翅、方法實(shí)現(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時(shí)調(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