问题描述:

在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中使用定位服务解决方案的更多相关文章

  1. iOS8中的定位服务

    iOS8中的定位服务 My app that worked fine in iOS 7 doesn't work with the iOS 8 SDK. CLLocationManager doesn ...

  2. iOS开发雕虫小技之傻瓜式定位神器-超简单方式解决iOS后台定时定位

    1.概述 由于公司一款产品的需求,最近一直在研究iOS设备的后台定位.主要的难点就是,当系统进入后台之后,程序会被挂起,届时定时器.以及代码都不会Run~ 所以一旦用户将我的App先换到了后台,我的定 ...

  3. iOS开发之动画中的时间(概况)

    一.引言 在iOS开发中使用动画时,可以通过设置动画的duration.speed.begintime.offset属性,来设置动画的时长.速度.起始时间及起始偏移. 用一个简单的例子来说明各个参数的 ...

  4. CentOS6.5(4)----宿主机无法访问虚拟机中的web服务解决方案

    宿主机无法访问虚拟机中的web服务 在Windows7宿主机中的VMware虚拟机中安装了CentOS6.5操作系统,并且基于Nginx搭建了Web服务器,网页刚刚搭建好的时候,通过宿主机的浏览器可以 ...

  5. iOS开发手记 - iOS9.3 UINavigationController添加后不显示storyboard中viewcontroller里的控件的解决方法

    我原先是这么做的,通常也是这么做 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSD ...

  6. (转)iOS 开发,工程中混合使用 ARC 和非ARC

    [前提知识] ARC:Automatic Reference Counting,自动引用计数 在开发 iOS 3 以及之前的版本的项目时我们要自己负责使用引用计数来管理内存,比如要手动 retain. ...

  7. iOS 开发,工程中混合使用 ARC 和非ARC(转)

    [前提知识] ARC:Automatic Reference Counting,自动引用计数 在开发 iOS 3 以及之前的版本的项目时我们要自己负责使用引用计数来管理内存,比如要手动 retain. ...

  8. iOS8 无法设置定位服务

    针对iOS8系统,需要在plist文件中添加这两个参数 NSLocationAlwaysUsageDescription = YES NSLocationWhenInUseUsageDescripti ...

  9. iOS开发关于xcode中souceControl的苹果文档翻译(节选)

    Subversion 1.7 provides many benefits: svn1.7版本有以下好处: Improved performance. Increased speed for many ...

随机推荐

  1. 【转】mysql 解事务锁

    ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction 原创 2014年07月31日 10:59:43 5 ...

  2. 谈互联网开放平台:“去中心化”大势所趋 zz

    文/磐石之心 几天前与好友聊到众筹咖啡馆的事情,他向我讲述了一个非常具有特色的众筹咖啡馆案例.而这个案例也引发我对当前互联网开放.去中心和集权的一些思考,今天就简单写出来与大家分享. 一个无赚钱目的的 ...

  3. Oracle启动两个监听

    接上篇:Oracle服务器修改IP后 Oracle服务器更换IP后,办公网络里面可以正常访问了,外地的同事,连了vpn 也可以访问,以为可以收工回家,突然又有同事过来说,机房的服务器ping不通新的i ...

  4. Github 快速上手实战教程

    一.实验介绍 1.1 实验内容 本次课程讲的是在实验楼的在线环境中,如何使用 Github 去管理在在线环境中使用的代码.配置.资源等实验相关文件,怎样去添加.同步和下拉在远程仓库中的实验文件,以此来 ...

  5. kafka 支持发布订阅

    概述 一般消息队列的是实现是支持两种模式的,即点对点,还有一种是topic发布订阅者模式,比如ACTIVEMQ.KAFKA也支持这两种模式,但是实现的原理不一样. KAFKA 的消息被读取后,并不是马 ...

  6. Tomcat入门

    1.JavaWeb概念 Java web,是用java技术来解决相关web互联网领域的技术的总称.web包括:web服务器和web客户端两部分.java在最早web客户端的应用有java applet ...

  7. s4-介质访问控制子层-1 MAC子层

    数据链路层被分成了两个子层:MAC和LLC MAC子层要解决什么问题? 介质访问控制(Madia Access Control) 数据通信方式 单播(unicast):One - to - One ...

  8. IntelliJ IDEA 2017版 spring-boot2.0.4+mybatis 自动部署的细节问题

    一.加载pom依赖包 <!--spring-boot开发热部署--> <dependency> <groupId>org.springframework.boot& ...

  9. typecho开启pjax,ajax,无刷新

    1.引入jquery和pjax 检查你的网站是否引入1.7.0版本以上的jquery.js,如果没有请全局引入 https://files.cnblogs.com/files/fan-bk/pjax. ...

  10. ArcGIS API for Silverlight部署本地地图服务

    这一节我们来讲新建立的ArcGIS API for Silverlight应用程序如何加载自己的地图服务的问题,网上的资料讲的都有点含糊不清,这次我们详细的讲一下配置的步骤: 首先介绍下我们的开发和部 ...