【问题标题】:Alert Box Asking User to Grant Location Privileges is not showing要求用户授予位置权限的警报框未显示
【发布时间】:2017-02-25 22:45:23
【问题描述】:

我刚刚开始在 iOS 上进行 SWIFT 编程。但是我在其他语言方面有很好的经验,例如 JAVA、C# 和 PHP。也就是说,我遇到了一点问题。我正在创建一个简单的应用程序,它将根据用户的地理位置显示用户信息。我正在关注here列出的教程。

但是,由于 Swift 2.x 和 3.x 之间发生了很多变化,我不得不修改他的代码。不太难,到目前为止我已经编译好了。但是,应用程序从未要求我允许我使用设备的位置。

我已按照 Apple 列出的指示 here 将“NSLocationAlwaysUsageDescription”放入 Info.plist。但是我从来没有被提示过。下面列出了应该显示此警报的相关代码。

任何帮助将不胜感激。谢谢。

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    // 1. status is not determined
    if CLLocationManager.authorizationStatus() == .notDetermined {
        locationManager.requestAlwaysAuthorization()
    }
        // 2. authorization were denied
    else if CLLocationManager.authorizationStatus() == .denied {
        let controller = UIAlertController(title: "Error!", message: "Location services were previously denied. Please enable location services for this app in Settings.", preferredStyle: .alert)

        let alertAction = UIAlertAction(title: "Dismiss", style: .destructive) { (action) in
            print("Dismiss button tapped!")
        }

        controller.addAction(alertAction)

    }
        // 3. we do have authorization
    else if CLLocationManager.authorizationStatus() == .authorizedAlways {
        locationManager.startUpdatingLocation()
    }
}

【问题讨论】:

  • 呸。你知道switch吗?
  • @matt 这不是生产代码。我只是想让 mapView 工作。我是 iOS 编程新手,所以请原谅这个问题。

标签: ios swift


【解决方案1】:

您的代码运行良好,您只需要显示警报:

self.present(controller, animated: true, completion: nil)

还可以看看我的回答 here 如何将 switch 用于此类代码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-17
    • 2015-07-24
    • 2014-11-18
    相关资源
    最近更新 更多