问题描述:

在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. 通俗理解 CPU && GPU

    CPU 力气大啥P事都能干,还要协调.GPU 上面那家伙的小弟,老大让他处理图形,这方面处理简单,但是量大,老大虽然能处理,可是老大只有那么几个兄弟,所以不如交给小弟处理了,小弟兄弟多,有数百至数千个 ...

  2. Tomcat架构解析(四)-----Coyote、HTTP、AJP、HTTP2等协议

    Connector是Tomcat中非常重要的一个组成部分,说白了,就是如何从客户端获取到相应的请求信息.这部分主要包括的难点有这样几个部分: 1.客户端与服务端的协议 客户端与服务端的协议是多种多样的 ...

  3. Educational Codeforces Round 51 D. Bicolorings(dp)

    https://codeforces.com/contest/1051/problem/D 题意 一个2*n的矩阵,你可以用黑白格子去填充他,求联通块数目等于k的方案数,答案%998244353. 思 ...

  4. mathematica入门学习记录:

    http://v.qq.com/vplus/4bc1736725fc7c3567d5bd9617482a49/foldervideos/m8k0000011aqj4k mathematica的数据 简 ...

  5. 总结一下《vue的使用》

    1.用vue创建项目的时候, 1.安装axios,对axios进行处理,创建axios.js文件,设置基础请求地址, 设置前置守卫和独享守卫,对请求数据进行设置,(特别实在进行token验证的时候特别 ...

  6. BZOJ 2440 [中山市选2011]完全平方数 (二分 + 莫比乌斯函数)

    2440: [中山市选2011]完全平方数 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 4805  Solved: 2325[Submit][Sta ...

  7. 如何制作一个自适应手机、电脑、ipad的网页方法总结

    进入2015年,手机上网的用户已经越来越多,已经赶超PC端.随着2G.3G.4G.免费WIFI和无线基站的不断普及,越来越多的人开始使用手机上网. 移动设备正超过桌面设备,成为访问互联网的最常见终端. ...

  8. 学以致用八---centos7.2 安装vim8+支持python3

    目的:打造基于python的vim环境 也是在地铁上突然产生的一个想法,平时都是在pycharm上练习python,但有时候会提示激活码过期,又得上网找激活码,够折腾的.那何不在linux环境下来搭建 ...

  9. C#版ObjectId

    近来在准备弄一个开源的HIS,但一周过去了几乎没有进度.就卡在ID如何生成.HIS的数据库压力大,如何多数据库支持,减轻压力一直想去实现.拿不准纯数字ID段还是GUID一类的文本ID.最终在mongo ...

  10. 一个支持邮件带附件群发的java类

    import java.io.UnsupportedEncodingException;import java.util.ArrayList;import java.util.Date;import ...