I have the info(Uuid, Major, Minor) of two beacons in my DB, and I want to create two CLBeaconRegion but dynamically, because there is the possibility of adding more beacons and create more CLBeaconRegion, I have this code:
for i in 0 ... self.ArrayBeaconIDs.count-1 {
let beaconRegion = CLBeaconRegion(
proximityUUID: UUID(uuidString: self.ArrayBeaconIDs[i])!,
major: CLBeaconMajorValue(self.ArrayMajor[i])! ,minor:CLBeaconMajorValue(self.ArrayMinor[i])!
, identifier: "teuchlitlan"
)
self.ArrayRegiones.append(beaconRegion)
}
for i in 0 ... self.ArrayRegiones.count-1 {
self.ArrayRegiones[i].notifyEntryStateOnDisplay = true
self.ArrayRegiones[i].notifyOnEntry = true
self.ArrayRegiones[i].notifyOnExit = true
self.beaconManager.startMonitoring(for: self.ArrayRegiones[i])
self.beaconManager.startRangingBeacons(in: self.ArrayRegiones[i])
self.beaconManager.requestState(for: self.ArrayRegiones[i])
}
but only detect the last region in the Array, How can I solve it?
When ranging or monitoring multiple regions, the identifier
field must be set to a unique string, as it is a key used to start, stop and replace the monitored region. If you try to monitor or range two different regions with the same identifier
field value, you effectively replace the previous region registration with the new one that has the same identifier
value.
To fix this in your case, simply change the identifier
value with a unique string. You can generate one like so:
OLD:
proximityUUID: UUID(uuidString: self.ArrayBeaconIDs[i])!,
major: CLBeaconMajorValue(self.ArrayMajor[i])!,
minor:CLBeaconMajorValue(self.ArrayMinor[i])!,
identifier: "teuchlitlan"
NEW:
proximityUUID: UUID(uuidString: self.ArrayBeaconIDs[i])!,
major: CLBeaconMajorValue(self.ArrayMajor[i])!,
minor:CLBeaconMajorValue(self.ArrayMinor[i])!,
identifier: "teuchlitlan\(i)"
The second code snippet will add a numeric suffix to each region identifier
so they are:
teuchlitlan0, teuchlitlan1, teuchlitlan2 ... etc