swift中判断设备是否打开定位
2015-12-21 20:26
411 查看
在基于LBS的项目中很多都会判断是否打开了定位功能.从而做提醒用户打开或者提醒到哪里打开等操作...在OC时候我们这么写:
//检测是否开启定位
if ([CLLocationManager locationServicesEnabled] &&
([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorized
|| [CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined)) {
//定位功能可用,开始定位
}
else if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied){
// NSlog("定位功能不可用,提示用户或忽略");
}
但是在swift中这样不好用,调用方式也不同,看官方文档上这么说:
看了文档就可以看出,其实就是调用方式不同,所以我们swift就如下写:
//检测是否开启定位
if ((CLLocationManager.authorizationStatus() == CLAuthorizationStatus.Authorized) || (CLLocationManager.authorizationStatus() == CLAuthorizationStatus.NotDetermined)) && (CLLocationManager .locationServicesEnabled()){
//定位开启了
}else if CLLocationManager.authorizationStatus() == CLAuthorizationStatus.Denied{
//定位没有开启
}
//检测是否开启定位
if ([CLLocationManager locationServicesEnabled] &&
([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorized
|| [CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined)) {
//定位功能可用,开始定位
}
else if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied){
// NSlog("定位功能不可用,提示用户或忽略");
}
但是在swift中这样不好用,调用方式也不同,看官方文档上这么说:
看了文档就可以看出,其实就是调用方式不同,所以我们swift就如下写:
//检测是否开启定位
if ((CLLocationManager.authorizationStatus() == CLAuthorizationStatus.Authorized) || (CLLocationManager.authorizationStatus() == CLAuthorizationStatus.NotDetermined)) && (CLLocationManager .locationServicesEnabled()){
//定位开启了
}else if CLLocationManager.authorizationStatus() == CLAuthorizationStatus.Denied{
//定位没有开启
}
相关文章推荐
- 项目开发笔记-2015.12.21-swift
- Swift Tutorial: Repeating Steps with Loops
- swift 新功能介绍
- Swift 已经正式开源了! 你了解到了吗?
- Swift 设置按钮圆角 代码&Xib
- Swift - 初次使用:
- swift禁止横屏
- swift 学习记录(静态属性)
- swift 基础知识
- swift学习记录(计算属性和属性观察者)
- swift学习记录(存储属性)
- <swift学习之路> UIAlertController的简单使用
- swift Touch ID 的简单使用 指纹识别
- swift 学习记录(类和结构体)
- 《从零开始学Swift》学习笔记(Day 50)——扩展计算属性、方法
- 《从零开始学Swift》学习笔记(Day 50)——扩展计算属性、方法
- swift学习记录(枚举)
- Swift 正式开源, 包括 Swift 核心库和包管理器
- Swift 控制流(笔记)
- Swift.cn Alpha聚乐部 总结