ios - CLLocationManagerDelegate 没有捕获事件

标签 ios swift

首先,我是 iOS 开发新手,所以请不要用 objective-c 示例向我解释。

我的问题是:
我完成了这门课:

import UIKit
import CoreLocation

class BeaconRadar: NSObject, CLLocationManagerDelegate {

    var manager : CLLocationManager!
    var region  : CLBeaconRegion!

    var seenError           = false
    var locationFixAchieved = false
    var locationStatus      = "Not Started"

    init() {
        super.init()

        var major   = CLBeaconMajorValue( 10 )
        var uuid    = NSUUID( UUIDString: "E2C56DB5-DFFB-48D2-B060-D0F5A71096E0" )
        var id      = "test"

        manager = CLLocationManager()
        region  = CLBeaconRegion( proximityUUID: uuid, major: major, identifier: id )

        manager.delegate        = self
        manager.desiredAccuracy = kCLLocationAccuracyBest

        manager.requestAlwaysAuthorization()
        manager.startRangingBeaconsInRegion(region)
    }

    func locationManager(manager: CLLocationManager!, didRangeBeacons beacons: AnyObject[], inRegion region: CLBeaconRegion! ){

        if (locationFixAchieved == false) {
            locationFixAchieved = true

            println(beacons)

        }

    }

    // If failed
    func locationManager(_manager: CLLocationManager!, rangingBeaconsDidFailForRegion region: CLBeaconRegion!, withError error: NSError!){

        manager.stopUpdatingLocation()
        if (error) {
            if (seenError == false) {
                seenError = true
                println("Error getting location")
            }
        }
    }

    //  // authorization status
    func locationManager(_manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus){
        println("2")
        var shouldIAllow = false

        switch status {
            case CLAuthorizationStatus.Restricted:
                locationStatus = "Restricted Access"
            case CLAuthorizationStatus.Denied:
                locationStatus = "User denied access"
            case CLAuthorizationStatus.NotDetermined:
                locationStatus = "Status not determined"
            default:
                locationStatus = "Allowed Access"
                shouldIAllow = true
        }

        NSNotificationCenter.defaultCenter().postNotificationName("LabelHasbeenUpdated", object: nil)
        if (shouldIAllow == true) {
            println("Location Allowed")
            // Start location services
            manager.startUpdatingLocation()
        } else {
            println("Denied access: \(locationStatus)")
        }
    }
}

我的 viewController 是:

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view, typically from a nib.

        var location = BeaconRadar()

    }

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

}

我期待在授权状态的实现函数中获得消息之一。 (BeaconRadar 中的最后一个)。但我一无所获。

如果我将在 viewController 中而不是在新类中实现所有这些方法 (即:

class ViewController: UIViewController, CLLocationManagerDelegate {
.....
}

然后它就可以正常工作了...

我错过了什么吗?

最佳答案

您已将 BeaconRadar 实例声明为 viewDidLoad 中的局部变量,因此一旦此函数退出,BeaconRadar 实例就会被释放。

您应该将 BeaconRegion 变量移出函数 -

class ViewController: UIViewController {

    let location = BeaconRadar()

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view, typically from a nib.

    }

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

}

关于ios - CLLocationManagerDelegate 没有捕获事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24585718/

相关文章:

ios - 如何删除其中有空格的键? ( swift )

jquery - 点击 Safari Mobile 中的突出显示和 jQuery on() 函数会产生巨大的突出显示

ios - Vuforia IOS 崩溃。由于内存问题而终止

ios - 如何在解析中从对象中获取 ObjectId 并在 swift 中将其显示为字符串

Swift 枚举,从关联的枚举中获取原始值

swift - 将 notificationCenter 观察器添加到特定的 NSTextField - MacOS

ios - 图像在动画之前跳跃

ios - NSDateFormatter dateFromString 返回错误月份的日期

ios - SKEmitterNode 将粒子速度更改为已发射的粒子

ios - 仅为特定 View 禁用手势识别器