加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Swift requestWhenInUseAuthorization无效

发布时间:2020-12-14 02:26:59 所属栏目:百科 来源:网络整理
导读:我正在使用 swift为iOS 9.3编写应用程序,需要一张地图来显示用户位置. 我初始化CLLocationManager及其委托,我配置info.plist 隐私 – 位置使用说明字符串和调用requestWhenInUseAuthorization但它不显示任何授权请求. 这是我的代码: import UIKitimport Map
我正在使用 swift为iOS 9.3编写应用程序,需要一张地图来显示用户位置.
我初始化CLLocationManager及其委托,我配置info.plist
隐私 – 位置使用说明字符串和调用requestWhenInUseAuthorization但它不显示任何授权请求.

这是我的代码:

import UIKit
import MapKit

class DetailController: UIViewController,CLLocationManagerDelegate {

    var locManager: CLLocationManager!

    @IBOutlet var myMap: MKMapView!

    override func viewDidLoad() {
        super.viewDidLoad()

        mostraPosizione()
    }

    func mostraPosizione(){
        locManager = CLLocationManager()
        locManager.delegate = self
        locManager.desiredAccuracy = kCLLocationAccuracyBest

        let authorizationStatus = CLLocationManager.authorizationStatus()

        if (authorizationStatus == CLAuthorizationStatus.NotDetermined) {
            locManager.requestWhenInUseAuthorization()
        } else {
            locManager.startUpdatingLocation()
        }


        myMap.showsUserLocation = true
    }

    func locManager(manager: CLLocationManager,didChangeAuthorizationStatus status: CLAuthorizationStatus) {

        if (status == CLAuthorizationStatus.NotDetermined) {
            locManager.requestWhenInUseAuthorization()

        } else {
            locManager.startUpdatingLocation()
        }
    }
}

和我的info.plist截图

Xcode输出:

Trying to start MapKit location updates without prompting for location authorization.
Must call -[CLLocationManager requestWhenInUseAuthorization]
or -[CLLocationManager requestAlwaysAuthorization] first.

我正在iPhone 5(iOS 9.3)模拟器上测试它.

这个东西有什么问题?
有人有建议吗?

我想是因为
let authorizationStatus = CLLocationManager.authorizationStatus()

是零,所以你的requestWhenInUseAuthorization()永远不会被调用.

let authorizationStatus = CLLocationManager.authorizationStatus()

    if (authorizationStatus == CLAuthorizationStatus.NotDetermined) || (authorizationStatus == nil) {
        locManager.requestWhenInUseAuthorization()
    } else {
        locManager.startUpdatingLocation()
    }

应该工作.

编辑:

如果用户拒绝请求,您还应该处理此案例

if (authorizationStatus == CLAuthorizationStatus.Denied ) {
  // Display a message,do what you want 
}

实际上,一旦用户拒绝授权,您就无法再次显示弹出窗口,用户必须进入应用程序设置并自行设置使用其位置的授权.但是,您现在可以直接从应用程序链接到设置,因此对用户来说更容易.

// Open the settings of your app
if let url = NSURL(string:UIApplicationOpenSettingsURLString) {
    UIApplication.sharedApplication().openURL(url)
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读