gpt4 book ai didi

ios - CLLocationManagerDelegate 没有捕获事件

转载 作者:行者123 更新时间:2023-11-28 13:22:01 31 4
gpt4 key购买 nike

首先,我是 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/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com