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 这 ...
随机推荐
- drupal7 获取网站名称
$site_name=variable_get('site_name', 'Drupal');
- reac——父组件向子组件传递值,子组件何时能同步获得父组件改变后的值
//这里是父组件的代码:export default class HeaderCom_son extends React.Component { constructor(props) { super( ...
- HTML 5 教程
HTML5 是下一代的 HTML.有必要再过一遍.看下要点. 具体看 http://www.w3school.com.cn/html5/index.asp 教程 和 HTML5 标签参考手册 ...
- 微服务架构之spring cloud eureka
Spring Cloud Eureka是spring cloud的核心组件,负责服务治理功能,起到中心枢纽作用,其它组件都依赖eureka来获取服务,然后再根据项目需求实现自己的业务,eureka在整 ...
- Totem协议(SRP/RRP)讲解
基本概念 •SRP: The Totem Single-Ring Ordering and MembershipProtocol –基于以太网的组通信协议,节点间组成单环结构 ...
- 如何在 Azure 中的经典 Windows 虚拟机上设置终结点
在 Azure 中使用经典部署模型创建的所有 Windows 虚拟机都可以通过专用网络通道与同一云服务或虚拟网络中的其他虚拟机自动通信. 但是,Internet 或其他虚拟网络中的计算机需要终结点将入 ...
- select 1 from table 语句中的1代表什么意思
在这里我主要讨论的有以下几个select 语句: doo_archive表是一个数据表,表的行数为4行,如下: 分别用三条select语句select 1 from doo_archive.sele ...
- 如何清除SQL 的登录名
复制路径 找到SqlStudio.bin 文件删除即可 SQL Server 2016 版本: C:\Users\%username%\AppData\Roaming\Microsoft\SQL Se ...
- django的orm操作的补充
---恢复内容开始--- 你在建立一个登陆的时候需要用到auth这个用户登陆模块 这个时候就需要用到user表中的信息你就可以继承 这个时候我们的用户表中继承我们user继承的表 ABstractUs ...
- 【转】ISMS方针、手册、程序文件模板
<ISMS方针.手册.程序文件模板> 1 信息安全管理手册 2 信息安全适用性声明 3 信息安全管理体系程序文件 3.01文件管理程序 3.02记录管理程序 3.03纠正措施管理程序 3. ...