【问题标题】:CLLocationManagerDelegate methods not being called in Swift code没有在 Swift 代码中调用 CLLocationManagerDelegate 方法
【发布时间】:2014-12-13 13:58:03
【问题描述】:

我正在尝试创建一个简单的应用程序来找出某人所在的区域,但我被卡住了,因为当应用程序运行并找到位置时,没有调用任何 CLLocationManagerDelegate 方法。

如果它是相关的,我也没有看到要求我授予应用使用位置权限的对话框。

这是我目前所拥有的 -

import UIKit
import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate {

@IBOutlet weak var locationLabel : UILabel!

var locationManager = CLLocationManager()
let geocoder = CLGeocoder ()

override func viewDidLoad() {
  super.viewDidLoad()
  // Do any additional setup after loading the view, typically from a nib.

  locationManager.delegate = self
  locationManager.desiredAccuracy = kCLLocationAccuracyBest
  locationManager.startUpdatingLocation()
}

override func viewDidDisappear(animated: Bool) {
  locationManager.stopUpdatingLocation()
}

override func didReceiveMemoryWarning() {
  super.didReceiveMemoryWarning()
  // Dispose of any resources that can be recreated.
}

func locationManager(manager: CLLocationManager!, didUpdateLocations locations:  [AnyObject]!) {
  geocoder.reverseGeocodeLocation(locations.last as CLLocation, completionHandler: {(placemark, error) in
    if (error != nil) {
      println("Error")
    } else {
      let pm = placemark.first as CLPlacemark

      println(pm)
    }
  })
}

func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
    println("Epic Fail")
  } 
}

我已经设置了断点,所以我知道代码永远不会被调用。我已经进去并在应用程序运行时手动打开了位置服务。

【问题讨论】:

标签: ios swift core-location cllocationmanager


【解决方案1】:

试着打电话

func locationManager(manager: CLLocationManager!, didUpdateToLocation newLocation: CLLocation!, fromLocation oldLocation: CLLocation!)

而不是您现在进行的委托调用... 我遇到了问题:

func locationManager(manager: CLLocationManager!, didUpdateLocations locations:  [AnyObject]!)

同样。但是使用上面的委托调用解决了我的问题。

编辑(没有正确阅读问题...)

您从不请求许可,这就是您没有收到弹出窗口的原因。调用以下命令: locationManager.requestWhenInUseAuthorization()

这是相当重要的一步,如果你没有请求用户授权你的应用程序,你的应用程序将无法运行

如果运行 iOS8,将 NSLocationWhenInUseUsageDescription 添加到 .plist 文件中也很重要。

截图:

【讨论】:

  • 感谢您的回答。我尝试了这两个建议,但我仍然看到相同的结果,即没有调用任何方法,也没有请求授权。我会补充一点,我是在设备而不是模拟器上运行它。
  • 好的 @Mark Reid,你运行的是哪个 iOS 版本?请记住,您必须将以下键/值添加到您的 .plist 文件中:NSLocationWhenInUseUsageDescription:“我需要为这个那个和另一个使用你的位置”
  • 这是 iOS 8,这就是我遇到的问题。谢谢。
  • 我也面临同样的问题。我已按照克里斯所说的所有步骤进行操作。你能告诉我你最终是如何解决问题的吗@Mark Reid
【解决方案2】:

下面的回答问题和答案给了我我需要的解决方案。

CLLocationManager authorization issue iOS 8

最终,如果您在 iOS 上运行,那么您必须添加密钥

NSLocationAlwaysUsageDescription(当请求位置数据始终可用时)

NSLocationWhenInUseUsageDescription(仅在您的应用运行时请求使用位置数据时)

如果您不这样做,则默认情况下您的应用将拒绝位置服务访问,并且不会提示用户允许访问。

【讨论】:

    【解决方案3】:
    private let loctionManager = CLLocationManager() // Here
    
    class YourViewController: ViewController {
        loctionManager.delegate = self
        loctionManager.requestWhenInUseAuthorization()
    }
    
    extension YourViewController: CLLocationManagerDelegate {
        func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
            if (status == .authorizedWhenInUse) {
                // do something
            }
        }
    }
    
    .plist
    Property List
    Key: Privacy - Location When In Use Usage Description
    Type: String
    Value: Use to add a location to your rap.
    OR
    Source code
    <key>NSLocationWhenInUseUsageDescription</key>
    <string>Use to add a location to your rap.</string>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-08
      • 2019-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-26
      • 2017-10-11
      相关资源
      最近更新 更多