问题描述:

在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. Linux网桥模式配置

    Linux网关模式下将有线LAN和无线LAN共享网段实现局域网内互联: 思路其实很简单:就是将虚拟出一个bridge口,将对应的有线LAN和无线LAN都绑定在这个虚拟bridge口上,并给这个brid ...

  2. Python图表绘制:matplotlib绘图库入门(转)

    matplotlib 是Python最著名的绘图库,它提供了一整套和matlab相似的命令API,十分适合交互式地行制图.而且也可以方便地将它作为绘图控件,嵌入GUI应用程序中. 它的文档相当完备,并 ...

  3. 关于Code Review

    为了保证代码质量,我们团队内部一直在推行Code Review.现在Code Review帮我们发现了很多代码的问题,提升了代码的可读性和质量,同时我们在Code Review上也花费了很多时间,有些 ...

  4. 通俗理解 CPU && GPU

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

  5. .NET性能优化(文摘)

    第1章 性能指标 1.1 性能目标 1.2 性能指标 第2章 性能度量 2.1 性能度量方式 白盒测试-小程序 黑盒测试-大型程序 2.2 Windows内置工具 2.2.1 性能计数器 2.2.2  ...

  6. D. Three Pieces(dp,array用法,棋盘模型)

    https://codeforces.com/contest/1065/problem/D 题意 给你一个方阵,里面的数字从1~nn,你需要从标号为1的格子依次走到标号为nn,在每一个格子你有两个决策 ...

  7. Android 从相机或相册或获取图片(转)

    参考: https://github.com/ASDbobo/GetPhotoDemo Android 8.0 调取系统摄像头和相册选择图片 9.3 使用Camera拍照

  8. 用JAVA实现大文件上传及显示进度信息

    一. 大文件上传基础描述: 各种WEB框架中,对于浏览器上传文件的请求,都有自己的处理对象负责对Http MultiPart协议内容进行解析,并供开发人员调用请求的表单内容. 比如: Spring 框 ...

  9. Python 之 Difflib

    Python 之 Difflib 2017年7月8日 word文档地址:https://wenku.baidu.com/view/36692440854769eae009581b6bd97f19237 ...

  10. Windows API编程(一)完整的示范程序

    ## #include <windows.h> LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);//回调函数; int APIEN ...