当iPhone在使用swift3的区域时,如何仅显示检测到的信标的项目一次

时间:2016-11-08 13:40:37

标签: ios swift3 beacon

我正在使用swift3构建iOS和WatchOS应用程序,它可以检测附近的Estimote信标,并在屏幕上显示链接到不同信标的项目表。一旦手机离开信标区域,链接到该信标的项目就会从屏幕上消失。我还希望每个项目仅在ONCE时显示,而不是在设备进入信标区域时定期显示。

所以,我做的是:

class ViewController: UIViewController , CLLocationManagerDelegate {  var locationManager : CLLocationManager!

override func viewDidLoad() {
    super.viewDidLoad()
    locationManager = CLLocationManager()
    locationManager.delegate = self
    locationManager.requestAlwaysAuthorization()
} //end didload

  /////////////////////////BEACON CODES////////////////////////

func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
    if status == .authorizedAlways {
        if CLLocationManager.isMonitoringAvailable(for: CLBeaconRegion.self) {
            if CLLocationManager.isRangingAvailable() {
                startScanning()
            }
        }
    }
}//end location manager  

func startScanning() { let uuid = UUID(uuidString:"B9407F30-F5F8-466E-AFF9-25556B57FE6D")  // I used the default UUID for all of the 3 beacons
    let beaconRegion = CLBeaconRegion(proximityUUID: uuid! , identifier: "MyBeacons")
     locationManager.startMonitoring(for: beaconRegion)
     locationManager.startRangingBeacons(in: beaconRegion)
}//end startscanning

func locationManager(_ manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], in region: CLBeaconRegion) {
 //to remove beacons with unknown
 let known = beacons.filter{ $0.proximity != CLProximity.unknown}
  if (known.count > 0){
        let uiRealm = try! Realm()
        var Beacons = uiRealm.objects(Beacon.self)
        var major = 0

       for beacon in beacons{
            for b in Beacons { //the beacon id in the DB is its major # and NOT the UUID
                major = (Int)(b.beaconID)!
               if major == (Int)(beacon.major) { //retrieve all items linked to this beacon, it could be one or many 
                    var items = uiRealm.objects(Item.self).filter("beacon.beaconID == '\(major)'")
                    print("items for beacon id = \(major) are :")
                    //print items
                    for i in items{
                        print (i.name)
                    } //end i
                }//end if major == beacon.major
            } //end b loop
        } //end beacon loop
     } //end if
    } //location manager method
}

输出:

items for beacon id = 50038 are :
Ibraheem, Asma

items for beacon id = 51802 are :
Shoug

items for beacon id = 28612 are :
Mona

items for beacon id = 50038 are :
Ibraheem, Asma

items for beacon id = 51802 are :
Shoug

items for beacon id = 28612 are :
Mona

items for beacon id = 51802 are :
Shoug

items for beacon id = 28612 are :
Mona

items for beacon id = 50038 are :
Ibraheem, Asma

items for beacon id = 28612 are :
Mona

items for beacon id = 50038 are :
Ibraheem, Asma

每秒打印一次项目(无限循环),但我想要的是每当手机进入该区域时,仅打印每个项目(例如:Mona应该只打印一次)。 此外,使用此代码,只要手机超出区域(这就是我想要的),该项目就会消失。

0 个答案:

没有答案