iOS 工具类 -- 定位

  • 开发中经常会遇到重复性的需求,自然会有重复性的代码。小白就会 command + c, command + v;大牛则会将重复的代码抽离出来,封装成工具类。一个好的工具类可以减少项目的代码量,避免代码冗余,并且提升你的开发速度。
  • 《 iOS 工具类 》系列持续更新,将自己封装的工具类分享给大家,可以直接贴代码到工程中使用。下面,iOS 工具类之定位。

一、准备

  • 谈到定位,除了代码还涉及到两处更改
1. Frameworks

添加 CoreLocation.framework 到工程中

2. Info.plist

配置 Info.plist

  1. 将 Info.plist 以 source code 形式打开,粘贴如下代码(根据需求看具体用那条,都粘上也没坏处)
    <key>NSLocationUsageDescription</key>
    <string>App需要您的同意,才能访问位置</string>
    <key>NSLocationAlwaysUsageDescription</key>
    <string>App需要您的同意,才能始终访问位置</string>
    <key>NSLocationWhenInUseUsageDescription</key>
    <string>App需要您的同意,才能在使用期间访问位置</string>
    key 一定不能写错,string 可以随便写

2.或者直接在列表下直接添加 NSLocationUsageDescription 为 Key,String 为 Type,Value 中看情况写

⚠️ iOS 10 之后的权限,如相机、麦克风等,需在 Info.plist 中进行配置

二、思路

1. 创建单例,并在 init 方法中初始化
2. 定义 block 及 getGps 方法
3. 调用 startUpdatingLocation 并通过 CLLocationCoordinate2D 获得经纬度

三、代码

  • 创建 QPLocationManager(名字随意),继承于 NSObject 的类
1. QPLocationManager.h
#import <Foundation/Foundation.h>

typedef void(^LocationBlock)(NSString * lon, NSString * lat);

@interface QPLocationManager : NSObject

+ (instancetype)sharedManager;

- (void)getGps:(LocationBlock)block;

@property (nonatomic, strong) NSString * lon;
@property (nonatomic, strong) NSString * lat;

@end
2. QPLocationManager.m
#import "QPLocationManager.h"
#import <CoreLocation/CoreLocation.h>

@interface QPLocationManager ()<CLLocationManagerDelegate>

@property (nonatomic, strong) CLLocationManager * locManager;

@property (nonatomic, copy) LocationBlock block;

@end

@implementation QPLocationManager

+ (instancetype)sharedManager {
    
    static QPLocationManager * _manager;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _manager = [[QPLocationManager alloc] init];
    });
    
    return _manager;
    
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        
        _locManager = [[CLLocationManager alloc] init];
        [_locManager setDesiredAccuracy:kCLLocationAccuracyBest];
        _locManager.distanceFilter = 100;
        _locManager.delegate = self;
        
        if (![CLLocationManager locationServicesEnabled]) {
            NSLog(@"请开启定位服务");
        } else {
            
            CLAuthorizationStatus status = [CLLocationManager authorizationStatus];

            if (status == kCLAuthorizationStatusNotDetermined) {
                [_locManager requestWhenInUseAuthorization];
            }
            
        }
    }
    return self;
}

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation {
    
    CLLocationCoordinate2D coor = newLocation.coordinate;
    
    NSString * lat = [NSString stringWithFormat:@"%@",@(coor.longitude)];
    NSString * lon = [NSString stringWithFormat:@"%@",@(coor.latitude)];
    
    [QPLocationManager sharedManager].lon = lon;
    [QPLocationManager sharedManager].lat = lat;
   
    self.block(lat,lon);
    
    [self.locManager stopUpdatingLocation];
  
}

- (void)getGps:(LocationBlock)block {
    
    self.block = block;
    [self.locManager startUpdatingLocation];
    
}

@end
3. 调用(在你需要的地方,引入头文件)
[[QPLocationManager sharedManager] getGps:^(NSString *lat, NSString *lon) {
        
    NSLog(@"%@,%@",lon, lat);
        
}];
  • 之前定义 block 的好处 ,将 block 变成型参,打出 getGps 只需两个回车,代码就出来了,效率大大的提高

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容