ios定位权限plist_[译] iOS 请求定位权限

作者:IOSCREATOR,原文链接,原文日期:2016-10-13

译者:冬瓜;校对: Cwift;定稿:CMB

在 iOS 中用户的位置信息被视为个人隐私,所以在获取时需要向用户请求权限。本篇教程将讲述向用户请求该权限的步骤。开发环境为 Xcode 8 Beta,运行环境为 iOS 10。

打开 Xcode 然后创建一个新的单视图应用(Single View Application)。

如图所示,点击 Next。将工程名命名为 IOS10RequestingPermissionTutorial,自行填写 Organization Name 和 Organization Identifier。选择 Swift 作为编程语言,适配设备选择 iPhone。

编辑 Storyboard。将一个按钮控件拖入主视图。双击按钮视图编辑文字改为 “Get Location”。如下图所示:

打开 Assistant Editor 并确保 ViewController.swift 文件可见。按住 Ctrl 键从按键控件拖拽到 ViewController 这个类中来创建一个 Action。

转到 ViewController.swift 文件并添加以下代码导入 Conre Location 框架。 import CoreLocation复制代码

让 ViewController 遵循 CLLocationManagerDelegate 协议。并修改该类的定义: class ViewController: UIViewController, CLLocationManagerDelegate {}复制代码

增加以下属性: let locationMgr = CLLocationManager()复制代码

CLLocationManager 是原生的 GPS 坐标管理对象。接下来按照以下代码来实现 getMyLocation 方法: @IBAction func getLocation() {

// 1

let status = CLLocationManager.authorizationStatus()

// 2

if status == .notDetermined {

locationMgr.requestWhenInUseAuthorization()

return

}

// 3

if status == .denied || status == .restricted {

let alert = UIAlertController(title: "Location Services Disabled", message: "Please enable Location Services in Settings", preferredStyle: .alert)

let okAction = UIAlertAction(title: "OK", style: .default, handler: nil)

alert.addAction(okAction)

present(alert, animated: true, completion: nil)

return

}

// 4

locationMgr.delegate = self

locationMgr.startUpdatingLocation()

}复制代码

authorizationStatus 对象将返回授权状态。

保证 app 在前台运行时,当定位更新后获取定位。

当定位服务被禁用时,用户将收到提示。

确定代理对象为当前的 ViewController

然后实现 CLLocationManager 的代理方法。 // 1

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

let currentLocation = locations.last!

print("Current location: \(currentLocation)")

}

// 2

func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {

print("Error \(error)")

}复制代码

当前位置坐标输出到控制台。

当定位无法更新时输出错误原因。

要在 app 运行时请求 GPS 定位权限,需要在 info.plist 中设置新的属性键(Key)。单击鼠标右键选择添加行,并输入以下值:

在 Build 并运行工程时,app 会主动寻求定位授权:

点开控制台中的位置箭头,选择一个预定义的位置。控制台会打印当前的 GPS 定位。 Current location: +/- 5.00m (speed -1.00 mps / course -1.00) @ 9/12/16, 3:25:27 PM Central European Summer Time

Current location: +/- 5.00m (speed -1.00 mps / course -1.00) @ 9/12/16, 3:25:28 PM Central European Summer Time

Current location: +/- 5.00m (speed -1.00 mps / course -1.00) @ 9/12/16, 3:25:29 PM Central European Summer Time

Current location: +/- 5.00m (speed -1.00 mps / course -1.00) @ 9/12/16, 3:25:30 PM Central European Summer Time

Current location: +/- 5.00m (speed -1.00 mps / course -1.00) @ 9/12/16, 3:25:31 PM Central European Summer Time

Current location: +/- 5.00m (speed -1.00 mps / course -1.00) @ 9/12/16, 3:25:32 PM Central European Summer Time复制代码

你可以在 Github 上的 ioscreator 仓库中下载 IOS10RequestingPermissionTutorial 的源码。

本文由 SwiftGG 翻译组翻译,已经获得作者翻译授权,最新文章请访问 swift.gg。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值