Reputation: 141
i have an App that does actions on didEnterRegion and didExitRegion of CLBeaconRegions. Those are both on my viewcontroller class. Now the user should be able to set the UUID by himself. Therefor I made a second view controller (setibeaconVC) to change the string of the UUID.
It does work however the regions increase overtime the user changes the UUID in the App. So the Actions are called first one time after changing the UUID once the actions are called two times and so on.
Is there any command to clear the cache of those regions before I change the UUID?
Here´s part of my code.
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()
}
And here the 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!
}
}
Many Thanks Stephan
Upvotes: 0
Views: 1740
Reputation: 1541
Before re-creating the CLBeaconRegion
in your view controller call locationManager.stopMonitoringForRegion(region)
.
You will need to check this contains a region before calling as on first run it won't. Easiest way to do this is to change region to an optional rather than forced unwrapped optional. Then check like this:
var region:CLBeaconRegion? // Update to this.
if let r = region {
locationManager.stopMonitoringForRegion(r)
}
Upvotes: 0