如何在swift didEnterRegion中获取UUID

时间:2017-04-03 08:07:40

标签: swift xcode bluetooth bluetooth-lowenergy ibeacon

func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {
    print("didEnterRegion")


    let beaconRegion = region as! CLBeaconRegion

    let content = UNMutableNotificationContent()

    content.title = NSString.localizedUserNotificationString(forKey: "I came", arguments: nil)
    content.body = NSString.localizedUserNotificationString(forKey: "My Information:\(region.identifier), major:\(beaconRegion.major!)/minor:\(beaconRegion.minor!)", arguments: nil)
    content.sound = UNNotificationSound.default()
    content.badge = UIApplication.shared.applicationIconBadgeNumber as NSNumber?;
    content.categoryIdentifier = "com.elonchan.localNotification"
    // Deliver the notification in five seconds.
    let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 1, repeats: false)
    let request = UNNotificationRequest.init(identifier: region.identifier, content: content, trigger: trigger)

    // Schedule the notification.
    let center = UNUserNotificationCenter.current()
    center.add(request)
    userdefalut.set(nowTime, forKey: "lastDate_\(tempUUID_2)")
    userdefalut.synchronize()
    print(UIDevice.current.identifierForVendor!.uuidString)
}

CLBeaconRegion(proximityUUID:UUID(uuidString:“00000000-0000-0000-0000-(name)”)!, major:208,minor:56,identifier:“(name)(path)”)

如何在didEnterRegion中获取UUID? 请帮助我..

2 个答案:

答案 0 :(得分:0)

你可以试试这个..

func locationManager(manager: CLLocationManager, didEnterRegion region: CLRegion) {
    manager.startRangingBeaconsInRegion((region as! CLBeaconRegion))
    var r = (region as! CLBeaconRegion)
    self.sendLocalNotificationWithMessage("\(r.proximityUUID)")
    self.sendLocalNotificationWithMessage("\(r.identifier)")
}

答案 1 :(得分:0)

您只需将CLRegion投射到CLBeaconRegion即可。像Swift 3中的这样:

let beaconRegion = region as! CLBeaconRegion

或者目标C:

CLBeaconRegion *beaconRegion = (CLBeaconRegion *)region;

执行此操作后,您可以访问CLBeaconRegion上的任何字段:

斯威夫特3:

NSLog("My UUID: \(beaconRegion.proximityUUID)")

目标C:

NSLog(@"My UUID: %@", beaconRegion.proximityUUID);