iOS 后台持续定位详解(支持ISO9.0以上)
iOS 后台持续定位详解(支持ISO9.0以上)
#import <CoreLocation/CoreLocation.h>并实现CLLocationManagerDelegate 代理,.h文件完整代码如下:
- #import <UIKit/UIKit.h>
- #import <CoreLocation/CoreLocation.h>
- @interface ViewController : UIViewController<CLLocationManagerDelegate>
- @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
- #import "ViewController.h"
- @interface ViewController (){
- CLLocationManager *locationManager;
- CLLocation *newLocation;
- CLLocationCoordinate2D coordinate;
- }
- @end
- @implementation ViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- [self initLocation];
- }
- #pragma mark 初始化定位
- -(void)initLocation {
- locationManager=[[CLLocationManager alloc] init];
- locationManager.delegate = self;
- locationManager.desiredAccuracy = kCLLocationAccuracyBest;//设置定位精度
- if([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0){
- [locationManager requestAlwaysAuthorization];
- }
// 9.0以后这个必须要加不加是不能实现后台持续定位的的
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 9.0) {
locationManager.allowsBackgroundLocationUpdates = YES;
}
- if(![CLLocationManager locationServicesEnabled]){
- NSLog(@"请开启定位:设置 > 隐私 > 位置 > 定位服务");
- }
- locationManager.pausesLocationUpdatesAutomatically = NO;
- [locationManager startUpdatingLocation];
- //[locationManager startMonitoringSignificantLocationChanges];
- }
- #pragma mark 定位成功
- -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
- newLocation = [locations lastObject];
- double lat = newLocation.coordinate.latitude;
- double lon = newLocation.coordinate.longitude;
- NSLog(@"lat:%f,lon:%f",lat,lon);
- }
- #pragma mark 定位失败
- -(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
- NSLog(@"error:%@",error);
- }
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- @end
iOS 后台持续定位详解(支持ISO9.0以上)的更多相关文章
- iOS原生地图开发详解
在上一篇博客中:http://my.oschina.net/u/2340880/blog/414760.对iOS中的定位服务进行了详细的介绍与参数说明,在开发中,地位服务往往与地图框架结合使用,这篇博 ...
- IOS—UITextFiled控件详解
IOS—UITextFiled控件详解 //初始化textfield并设置位置及大小 UITextField *text = [[UITextField alloc]initWithFrame:CGR ...
- iOS中—触摸事件详解及使用
iOS中--触摸事件详解及使用 (一)初识 要想学好触摸事件,这第一部分的基础理论是必须要学会的,希望大家可以耐心看完. 1.基本概念: 触摸事件 是iOS事件中的一种事件类型,在iOS中按照事件划分 ...
- ios新特征 ARC详解
IOS ARC 分类: IOS ARC2013-01-17 09:16 2069人阅读 评论(0) 收藏 举报 目录(?)[+] 关闭工程的ARC(Automatic Reference Co ...
- Xamarin 后台持续定位与提示
IOS后台持续运行对于c#程序员不懂得ios后台机制的是存在一定困扰的.特别是ios9过后对后台和安全进行了更严格的限制 好了废话不多说 一 设置info.plist权限信息 参考: 后台模式:htt ...
- iOS 单元测试之XCTest详解(一)
iOS 单元测试之XCTest详解(一) http://blog.csdn.net/hello_hwc/article/details/46671053 原创blog,转载请注明出处 blog.csd ...
- iOS学习之UINavigationController详解与使用(一)添加UIBarButtonItem
http://blog.csdn.net/totogo2010/article/details/7681879 1.UINavigationController导航控制器如何使用 UINavigati ...
- [转]iOS学习之UINavigationController详解与使用(三)ToolBar
转载地址:http://blog.csdn.net/totogo2010/article/details/7682641 iOS学习之UINavigationController详解与使用(二)页面切 ...
- IOS 友盟使用详解
IOS 友盟使用详解 这篇博客将会详细介绍友盟的使用,希望对博友们有所帮助. 首先我们在浏览器上搜索友盟. 在这里我们选择官网这个,进去友盟官网后我们按照下图进行选择. 接下来选择如下图 Next 这 ...
随机推荐
- is_array判断是否为数组
if(is_array($arr)){ echo "是数组"; }else{ echo "不是数组"; }
- Android Editable
在android的sdk中有讲,“This is the interface for text whose content and markup can be changed (as opposed ...
- android头像上传(获取头像加剪切)
因为项目中需要用到头像上传的功能,所以就下个Ddmo先来实现下. demo我是类似仿微信的,在一个GridView中展示所有的图片,其中第一个item可以去照相:获取到图片后再进行剪切. 图片的剪切是 ...
- 2.MySQL 数据类型
MySQL 数据类型 MySQL中定义数据字段的类型对你数据库的优化是非常重要的. MySQL支持多种类型,大致可以分为三类:数值.日期/时间和字符串(字符)类型. 数值类型 MySQL支持所有标准S ...
- java 中sendredirect()和forward()方法的区别
一.文章1 HttpServletResponse.sendRedirect与RequestDispatcher.forward方法都可以实现获取相应URL资源. sendRedirect实现请求重 ...
- 为什么说对象字面量赋值比new Object()高效?
http://www.cnblogs.com/mushishi/p/5811743.html
- security权限控制
目录 前言 数据库和dimain 静态页面 配置文件 web.xml引入 service校验方法 用户名的获取 不同角色访问控制权限 jsp页面 后台 前言 spring自带角色权限控制框架 用户-角 ...
- SQL语句的执行顺序 1>优先执行,然后依数字排序
1>…From 表 2>…Where 条件 3>…Group by 列 4>…Having 筛选条件 ...
- TMG 2010 为HTTPS协议添加非标准端口(443)
1.添加加密端口时,编辑脚本addsslports.vbs addsslports.vbs 脚本内容如下: Dim root Dim tpRanges Dim newRange Set root = ...
- Spark 2.x 中 Sort-Based Shuffle 产生的内幕
本课主题 Sorted-Based Shuffle 的诞生和介绍 Shuffle 中六大令人费解的问题 Sorted-Based Shuffle 的排序和源码鉴赏 Shuffle 在运行时的内存管理 ...