iOS 后台持续定位详解(支持ISO9.0以上)

#import <CoreLocation/CoreLocation.h>并实现CLLocationManagerDelegate 代理,.h文件完整代码如下:

  1. #import <UIKit/UIKit.h>
  2. #import <CoreLocation/CoreLocation.h>
  3. @interface ViewController : UIViewController<CLLocationManagerDelegate>
  4. @end

2.info.list文件:

右键,Add Row,添加的Key为NSLocationAlwaysUsageDescription,其它值默认,示例如下:

3.添加后台定位权限

4.ViewController.m 文件:

(1)定义一个私有CLLocationManager对象

(2)初始化并设置参数(initLocation方法),其中

locationManager.desiredAccuracy设置定位精度,有六个值可选,精度依次递减

kCLLocationAccuracyBestForNavigation

kCLLocationAccuracyBest

kCLLocationAccuracyNearestTenMeters

kCLLocationAccuracyHundredMeters

kCLLocationAccuracyKilometer

kCLLocationAccuracyThreeKilometers

locationManager.pausesLocationUpdatesAutomatically 设置是否允许系统自动暂停定位,这里要设置为NO,刚开始我没有设置,后台定位持续20分钟左右就停止了!

(3)实现CLLocationManagerDelegate的代理方法,此方法在每次定位成功后调用:

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray*)locations;

*也可以通过实现以下方法:

- (void)locationManager:(CLLocationManager *)manager

didUpdateToLocation:(CLLocation *)newLocation

fromLocation:(CLLocation *)oldLocation

(4)实现CLLocationManagerDelegate的代理方法,此方法在定位出错后调用:

-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error

 
.m文件完整代码如下:
  1. #import "ViewController.h"
  2. @interface ViewController (){
  3. CLLocationManager *locationManager;
  4. CLLocation *newLocation;
  5. CLLocationCoordinate2D coordinate;
  6. }
  7. @end
  8. @implementation ViewController
  9. - (void)viewDidLoad {
  10. [super viewDidLoad];
  11. [self initLocation];
  12. }
  13. #pragma mark 初始化定位
  14. -(void)initLocation {
  15. locationManager=[[CLLocationManager alloc] init];
  16. locationManager.delegate = self;
  17. locationManager.desiredAccuracy = kCLLocationAccuracyBest;//设置定位精度
  18. if([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0){
  19. [locationManager requestAlwaysAuthorization];
  20. }
  21. // 9.0以后这个必须要加不加是不能实现后台持续定位的的

    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 9.0) {

    locationManager.allowsBackgroundLocationUpdates = YES;

    }

  22. if(![CLLocationManager locationServicesEnabled]){
  23. NSLog(@"请开启定位:设置 > 隐私 > 位置 > 定位服务");
  24. }
  25. locationManager.pausesLocationUpdatesAutomatically = NO;
  26. [locationManager startUpdatingLocation];
  27. //[locationManager startMonitoringSignificantLocationChanges];
  28. }
  29. #pragma mark 定位成功
  30. -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
  31. newLocation = [locations lastObject];
  32. double lat = newLocation.coordinate.latitude;
  33. double lon = newLocation.coordinate.longitude;
  34. NSLog(@"lat:%f,lon:%f",lat,lon);
  35. }
  36. #pragma mark 定位失败
  37. -(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
  38. NSLog(@"error:%@",error);
  39. }
  40. - (void)didReceiveMemoryWarning {
  41. [super didReceiveMemoryWarning];
  42. // Dispose of any resources that can be recreated.
  43. }
  44. @end

iOS 后台持续定位详解(支持ISO9.0以上)的更多相关文章

  1. iOS原生地图开发详解

    在上一篇博客中:http://my.oschina.net/u/2340880/blog/414760.对iOS中的定位服务进行了详细的介绍与参数说明,在开发中,地位服务往往与地图框架结合使用,这篇博 ...

  2. IOS—UITextFiled控件详解

    IOS—UITextFiled控件详解 //初始化textfield并设置位置及大小 UITextField *text = [[UITextField alloc]initWithFrame:CGR ...

  3. iOS中—触摸事件详解及使用

    iOS中--触摸事件详解及使用 (一)初识 要想学好触摸事件,这第一部分的基础理论是必须要学会的,希望大家可以耐心看完. 1.基本概念: 触摸事件 是iOS事件中的一种事件类型,在iOS中按照事件划分 ...

  4. ios新特征 ARC详解

    IOS ARC 分类: IOS ARC2013-01-17 09:16 2069人阅读 评论(0) 收藏 举报   目录(?)[+]   关闭工程的ARC(Automatic Reference Co ...

  5. Xamarin 后台持续定位与提示

    IOS后台持续运行对于c#程序员不懂得ios后台机制的是存在一定困扰的.特别是ios9过后对后台和安全进行了更严格的限制 好了废话不多说 一 设置info.plist权限信息 参考: 后台模式:htt ...

  6. iOS 单元测试之XCTest详解(一)

    iOS 单元测试之XCTest详解(一) http://blog.csdn.net/hello_hwc/article/details/46671053 原创blog,转载请注明出处 blog.csd ...

  7. iOS学习之UINavigationController详解与使用(一)添加UIBarButtonItem

    http://blog.csdn.net/totogo2010/article/details/7681879 1.UINavigationController导航控制器如何使用 UINavigati ...

  8. [转]iOS学习之UINavigationController详解与使用(三)ToolBar

    转载地址:http://blog.csdn.net/totogo2010/article/details/7682641 iOS学习之UINavigationController详解与使用(二)页面切 ...

  9. IOS 友盟使用详解

    IOS 友盟使用详解 这篇博客将会详细介绍友盟的使用,希望对博友们有所帮助. 首先我们在浏览器上搜索友盟. 在这里我们选择官网这个,进去友盟官网后我们按照下图进行选择. 接下来选择如下图 Next 这 ...

随机推荐

  1. MySQL数据库更改默认引擎为Innodb【配置】

    InnoDB和MyISAM是许多人在使用MySQL时最常用的两个表类型,这两个表类型各有优劣,视具体应用而定. 基本的差别为:MyISAM类型不支持事务处理等高级处理,而InnoDB类型支持.MyIS ...

  2. Java设计模式—解释器模式&迭代器模式简介

       解释器模式在实际的系统开发中使用得非常少,因为它会引起效率.性能以及维护等问题,一般在大中型的框架型项目能够找到它的身影,如一些数据分析工具.报表设计工具.科学计算工具等,若你确实遇到" ...

  3. 从零开始安装、编译、部署 Docker

    简介 主要介绍如何从基础系统debian部署docker关于docker基础知识在 相关资料 里有链接 安装docker 1.使用root用户身份添加apt源添加public key使docker的安 ...

  4. BottomNavigationView结合ViewPager

    BottomNavigationView是Google推出的底部导航栏组件,在没有这些底部导航组件之前,Android开发者多使用的是RadioGroup,在上一个项目开发中我们使用了Google的B ...

  5. ExpressRoute 常见问题

    什么是 ExpressRoute? ExpressRoute 是一项 Azure 服务,允许在 Microsoft 数据中心与本地环境或共同租用设施中的基础结构之间创建专用连接. ExpressRou ...

  6. 数据分析之pandas常见的数据处理(四)

    常见聚合方法 方法 说明 count 计数 describe 给出各列的常用统计量 min,max 最大最小值 argmin,argmax 最大最小值的索引位置(整数) idxmin,idxmax 最 ...

  7. Linux traceroute命令详解

    traceroute我们可以知道信息从你的计算机到互联网另一端的主机是走的什么路径.当然每次数据包由某一同样的出发点(source)到达某一同样的目的地(destination)走的路径可能会不一样, ...

  8. [T-ARA][거짓말(Part.1)][谎言(Part.1)]

    歌词来源:http://music.163.com/#/song?id=5403062 作曲 : 赵英秀 [作曲 : 赵英秀] 作词 : 安英民 [作词 : 安英民] 사랑한단 거짓말 보고싶을거란 ...

  9. html转canvas html2canvas.js

    $("#btn-html2canvas").on("click",function(){//btn-html2canvas为按钮 //content-main为 ...

  10. ERROR: Repository not found. ////Git, but is not registered in the Settings.

    1.ERROR: Repository not found. 这个问题是因为在你推送的github账户中,并没有这个Repository. 解决方法: 1)检查自己的github中的Repositor ...