iOS开发手记-iOS8中使用定位服务解决方案
问题描述:
在iOS8之前,app第一次开始定位服务时,系统会弹出一个提示框来让用户选择是否允许使用定位信息。但iOS8后,app将不会出现这个弹窗。第一次运行之后,在设置->隐私->定位服务中,你的app没有任何设置,既不是“永不”,也不是“始终”。
代码如下:
#import "XYZFirstViewController.h" @interface XYZFirstViewController ()
- (IBAction)LocateButtonClick:(id)sender;
@end @implementation XYZFirstViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(startLocate) name:@"startLocateNotification" object:nil];
_locationManager=[[CLLocationManager alloc] init];
_locationManager.delegate=self;
_locationManager.desiredAccuracy=kCLLocationAccuracyBest;
_locationManager.distanceFilter=1000.0f;
_mapView.mapType=MKMapTypeStandard;
_mapView.delegate=self; } - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} -(void) viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[_locationManager startUpdatingLocation];
} -(void) viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[_locationManager stopUpdatingLocation];
} -(void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *currentLocation=[locations lastObject];
_currentLocation=currentLocation;
self.currentLocationLabel.text=[NSString stringWithFormat:@"%3.5f,%3.5f,%3.5f", currentLocation.coordinate.longitude,currentLocation.coordinate.latitude,currentLocation.altitude];
MKCoordinateRegion region=MKCoordinateRegionMakeWithDistance(currentLocation.coordinate, , );
[_mapView setRegion:region animated:YES];
MKPointAnnotation *point=[[MKPointAnnotation alloc] init];
point.coordinate=_currentLocation.coordinate;
point.title=@"my location";
[_mapView addAnnotation:point];
} -(void) locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"error:%@",error);
} /*
#pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/ - (IBAction)LocateButtonClick:(id)sender {
[[NSNotificationCenter defaultCenter] postNotificationName:@"startLocateNotification" object:self ];
} -(void) startLocate
{
CLGeocoder *geocoder=[[CLGeocoder alloc]init];
[geocoder reverseGeocodeLocation:_currentLocation completionHandler:^(NSArray *placeMarks, NSError *error)
{
if([placeMarks count]>)
{
NSLog(@"%@",placeMarks);
CLPlacemark *placeMark=placeMarks[];
NSDictionary *addressDictonary=placeMark.addressDictionary;
_currentAddressLabel.text=[NSString stringWithFormat:@"%@,%@,%@",[addressDictonary objectForKey:(NSString *)kABPersonAddressStateKey],[addressDictonary objectForKey:(NSString *)kABPersonAddressCityKey],[addressDictonary objectForKey:(NSString *) kABPersonAddressStreetKey] ];
}
}]; }
@end
解决方案:
以上代码在iOS8之后需要手动调用CLLocationManager对象的requestAlwaysAuthorization/
requestWhenInUseAuthorization方法。 调用该方法需要在Info.plist中设置NSLocationAlwaysUsageDescription/NSLocationWhenInUseUsageDescription的值,这个值会显示在系统提示框中。
代码如下:
-(void) viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[_locationManager requestWhenInUseAuthorization];
[_locationManager startUpdatingLocation];
}
info.plist设置如下:

允许效果:


iOS开发手记-iOS8中使用定位服务解决方案的更多相关文章
- iOS8中的定位服务
iOS8中的定位服务 My app that worked fine in iOS 7 doesn't work with the iOS 8 SDK. CLLocationManager doesn ...
- iOS开发雕虫小技之傻瓜式定位神器-超简单方式解决iOS后台定时定位
1.概述 由于公司一款产品的需求,最近一直在研究iOS设备的后台定位.主要的难点就是,当系统进入后台之后,程序会被挂起,届时定时器.以及代码都不会Run~ 所以一旦用户将我的App先换到了后台,我的定 ...
- iOS开发之动画中的时间(概况)
一.引言 在iOS开发中使用动画时,可以通过设置动画的duration.speed.begintime.offset属性,来设置动画的时长.速度.起始时间及起始偏移. 用一个简单的例子来说明各个参数的 ...
- CentOS6.5(4)----宿主机无法访问虚拟机中的web服务解决方案
宿主机无法访问虚拟机中的web服务 在Windows7宿主机中的VMware虚拟机中安装了CentOS6.5操作系统,并且基于Nginx搭建了Web服务器,网页刚刚搭建好的时候,通过宿主机的浏览器可以 ...
- iOS开发手记 - iOS9.3 UINavigationController添加后不显示storyboard中viewcontroller里的控件的解决方法
我原先是这么做的,通常也是这么做 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSD ...
- (转)iOS 开发,工程中混合使用 ARC 和非ARC
[前提知识] ARC:Automatic Reference Counting,自动引用计数 在开发 iOS 3 以及之前的版本的项目时我们要自己负责使用引用计数来管理内存,比如要手动 retain. ...
- iOS 开发,工程中混合使用 ARC 和非ARC(转)
[前提知识] ARC:Automatic Reference Counting,自动引用计数 在开发 iOS 3 以及之前的版本的项目时我们要自己负责使用引用计数来管理内存,比如要手动 retain. ...
- iOS8 无法设置定位服务
针对iOS8系统,需要在plist文件中添加这两个参数 NSLocationAlwaysUsageDescription = YES NSLocationWhenInUseUsageDescripti ...
- iOS开发关于xcode中souceControl的苹果文档翻译(节选)
Subversion 1.7 provides many benefits: svn1.7版本有以下好处: Improved performance. Increased speed for many ...
随机推荐
- 2018.11.06 bzoj1040: [ZJOI2008]骑士(树形dp)
传送门 由题可知给出的是基环森林. 因此对于每个基环森林找到环断开dpdpdp两次就行了. 代码: #include<bits/stdc++.h> using namespace std; ...
- ext中对json数据的处理解析
看贴:http://blog.csdn.net/xieshengjun2009/article/details/5959687
- IntelliJ IDEA 2017版 Spring5 java.lang.NoSuchMethodError: org.springframework.boot.SpringApplication.<init>([Ljava/lang/Object;)V
错误是java.lang.NoSuchMethodError: org.springframework.boot.SpringApplication.<init>([Ljava/lang/ ...
- oss+上传文件夹
最近公司做工程项目,实现文件夹云存储上传. 网上找了一天,发现网上很多代码都存在相似问题,最后终于找到了一个满足我需求的项目. 工程如下: 这里对项目的文件夹传输功能做出分析,怎么实现文件夹上传的,如 ...
- 16-margin的用法
margin塌陷问题 当时说到了盒模型,盒模型包含着margin,为什么要在这里说margin呢?因为元素和元素在垂直方向上margin里面有坑. 我们来看一个例子: html结构: <div ...
- Spring的下载与安装
Spring是一个独立的框架,不依赖于任何Web服务器或容器.它既可在独立的JavaSE项目中使用,也可以在Java Web项目中使用. 下载和安装Spring框架可按如下步骤进行: 1.登录http ...
- MyBatis(二)最简易的增、删、改、查
这篇是承接上一篇的helloWorld程序.首先将一系列初始化的步骤再做一个工厂类进行包装,代码如下: package com.tinaluo.sun; import java.io.InputStr ...
- (DP 雷格码)Gray code -- hdu -- 5375
http://acm.hdu.edu.cn/showproblem.php?pid=5375 Gray code Time Limit: 2000/1000 MS (Java/Others) M ...
- java基础-day28
第05天 MySQL 今日内容介绍 u 数据库的概述 u MySQL和SQLyog的安装和使用 u SQL语句 第1章 数据库的概述 1.1 数据库的概述 1.1.1 概述 l 什么是数据库 ...
- Java关键字、标识符、表达式、常用符号
关键字 包:package(包).import(导入) 类:class(类).enum(枚举).interface(接口).extends(继承).implements(实现) 方法:void(空). ...