博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS8中的定位服务
阅读量:5891 次
发布时间:2019-06-19

本文共 2140 字,大约阅读时间需要 7 分钟。

iOS8中的定位服务

My app that worked fine in iOS 7 doesn't work with the iOS 8 SDK.

CLLocationManager doesn't return a location, and I don't see my app under Settings > Location Services either. I did a Google search on the issue but nothing came up, what could be wrong?

我的应用在iOS7上运行得好好的,但是到了iOS8上就出问题了.

在iOS8中,CLLocationManager并没有返回一个地址,而且,我也没有在Settings>Location Services中找到该服务,在Google上也没查到类似的问题,请问到底哪里出问题了?

 

I ended up solving my own problem.

Apparently in iOS 8 SDK, requestAlwaysAuthorization (for background location) or requestWhenInUseAuthorization (location only when foreground) call on CLLocationManager is needed before starting location updates.

There also needs to be NSLocationAlwaysUsageDescription or NSLocationWhenInUseUsageDescription key in Info.plist with a message to be displayed in the prompt. Adding these solved my problem.

Hope it helps someone else.

最后我解决了这个问题.

在iOS8的sdk中,你需要额外的requestAlwaysAuthorization(用作后台定位)以及requestWhenInUseAuthorization(用作前台定位),在调用CLLocationManager更新地址之前你都需要调用上述两个方法.你还需要在Info.plist文件中额外的添加NSLocationAlwaysUsageDescription以及NSLocationWhenInUseUsageDescription,这么做了之后才解决了我的问题.

 

According to the Apple docs:

As of iOS 8, the presence of a NSLocationWhenInUseUsageDescription or a NSLocationAlwaysUsageDescription key value in your app's Info.plist file is required. It's then also necessary to request permission from the user prior to registering for location updates, either by calling [self.myLocationManager requestWhenInUseAuthorization] or [self.myLocationManager requestAlwaysAuthorization] depending on your need. The string you entered into the Info.plist will then be displayed in the ensuing dialog.

If the user grants permission, it's business as usual. If they deny permission, then the delegate is not informed of location updates.

根据苹果的官方文档:

在iOS8上,你需要在你的plist文件中添加新的键值NSLocationWhenInUseUsageDescription或者NSLocationAlwaysUsageDescription.然后,每次定位的时候你都需要获取用户的授权信息,通过调用[self.myLocationManager requestWhenInUseAuthorization] 或者[self.myLocationManager requestAlwaysAuthorization],至于调用哪一个,这就根据你自己的需求了.那个你添加进plist文件中的键值信息会在确认的信息中有提示.

如果用户允许你定位,你可以像往常那样子使用定位功能.如果他们拒绝了,那么,这个定位的更新更能永远也不会被执行.

转载地址:http://sabsx.baihongyu.com/

你可能感兴趣的文章
/dev/mem可没那么简单【转】
查看>>
linux非阻塞的socket EAGAIN的错误处理【转】
查看>>
编写第一个Shell脚本【TLCL】
查看>>
《C语言》-(流程控制:顺序结构、选择结构、循环结构)
查看>>
《Objective-c》Foundation框架 -(练习:计算整个文件夹的代码行数)
查看>>
Linux学习之路(三)Shell脚本初探
查看>>
测试人员应该具备哪些技能
查看>>
洛谷 P1440 求m区间内的最小值
查看>>
怎样让pl sql developer 界面视图复位
查看>>
hibernate manytomany
查看>>
A+B Problem II
查看>>
linux信号
查看>>
微信js分享第三方链接
查看>>
linux
查看>>
pandas按索引插入对应值的处理方法 - join
查看>>
人月神话读后感(1)
查看>>
CSS3权威指南-浮动2
查看>>
mysql ERROR 1018 (HY000): Can't read dir of '.' (errno: 24)
查看>>
Swift3中函数的使用
查看>>
0044-邮局汇款
查看>>