swift locationManager清除区域缓存

时间:2016-08-17 22:57:53

标签: ios swift

我有一个应用程序,它对didEnterRegion和didExitRegion的CLBeaconRegions执行操作。这些都在我的viewcontroller类上。现在用户应该能够自己设置UUID。因此我制作了第二个视图控制器(setibeaconVC)来更改UUID的字符串。

它确实有效但是区域增加了用户更改应用程序中的UUID。因此,一旦操作被调用两次,就会在更改UUID后首次调用Actions。等等。

在更改UUID之前是否有任何命令可以清除这些区域的缓存?

这是我的代码的一部分。

的ViewController:

var ibeaconuuid:String! = "B9407F30-F5F8-466E-AFF9-25556B57FE6D"

let locationManager = CLLocationManager()

var beaconUUID:NSUUID!
var region:CLBeaconRegion!

override func viewDidLoad() {
    super.viewDidLoad()


    beaconUUID = NSUUID(UUIDString: ibeaconuuid)
    region = CLBeaconRegion(proximityUUID: beaconUUID, identifier: "Beacon")

    locationManager.delegate = self
    locationManager.pausesLocationUpdatesAutomatically = false
    region.notifyOnEntry = true
    region.notifyOnExit = true

    region.notifyEntryStateOnDisplay = false


    if (CLLocationManager.authorizationStatus() != CLAuthorizationStatus.AuthorizedAlways) {
        locationManager.requestAlwaysAuthorization()
    }


    if (CLLocationManager .isMonitoringAvailableForClass(CLBeaconRegion))
    {
        print("OK")
    }
    else {
        print("Problem")

    }

    locationManager.startMonitoringForRegion(region)
    locationManager.startRangingBeaconsInRegion(region)
    locationManager.startUpdatingLocation()
}

这里是setiBeaconVC:

class setibeaconVC: UIViewController {


@IBOutlet weak var ibeaconuuidtext: UITextField!


override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}

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

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    let DestViewController: ViewController = segue.destinationViewController as! ViewController
    DestViewController.ibeaconuuid = ibeaconuuidtext.text!
}

}

非常感谢 斯蒂芬

1 个答案:

答案 0 :(得分:0)

在视图控制器中重新创建CLBeaconRegion之前,请致电locationManager.stopMonitoringForRegion(region)

你需要在调用之前检查它包含一个区域,因为在第一次运行时它不会。最简单的方法是将区域更改为可选而不是强制解包的可选项。然后像这样检查:

var region:CLBeaconRegion? // Update to this.

if let r = region {
    locationManager.stopMonitoringForRegion(r)
}