可以在前台和后台检测到信标
步骤 :- 1 info.plist 中的更改
在 info.plist 中,您需要更改“使用说明”。
为此添加NSLocationAlwaysUsageDescription
字符串类型,其中包含您的消息。
步骤 :- 2 对于后台模式
在 App Delegate 中添加以下代码:
var window: UIWindow?
var locationManager:CLLocationManager = CLLocationManager()
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
locationManager.delegate = self
locationManager.requestAlwaysAuthorization()
// Request permission to send notifications
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options:[.alert, .sound]) { (granted, error) in }
return true
}
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
rangeBeacons()
}
func rangeBeacons(){
let uuid = UUID(uuidString: "3e1d7817-4eac-4b27-b809-deee2f246c46")
//let uuid = UUID(uuidString: "8492E75F-4FD6-469D-B132-043FE94921D8")
let major:CLBeaconMajorValue = 1
let minor:CLBeaconMinorValue = 2
let identifier = "myBeacon"
let region = CLBeaconRegion(proximityUUID: uuid!, major: major, minor: minor, identifier: identifier)
region.notifyOnEntry = true
region.notifyEntryStateOnDisplay = true
region.notifyOnExit = true
locationManager.startRangingBeacons(in: region)
locationManager.startMonitoring(for: region)
}
func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {
let content = UNMutableNotificationContent()
content.title = "Hello!!!"
content.body = "You Are Back in the Office"
content.sound = .default()
let request = UNNotificationRequest(identifier: "SufalamTech", content: content, trigger: nil)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
}
func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion) {
let content = UNMutableNotificationContent()
content.title = "Alert!!!"
content.body = "You are Out of the Office"
content.sound = .default()
let request = UNNotificationRequest(identifier: "identifier", content: content, trigger: nil)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
}
另外不要忘记添加CLLocationManagerDelegate
并导入CoreLocations
and UserNotifications
步骤 :- 3 对于前台或活动模式
在ViewController中添加以下代码
func locationManager(_ manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], in region: CLBeaconRegion) {
guard let discoveredbeaconProximity = beacons.first?.proximity else {print("Beacons Cannot be located"); return }
if ((discoveredbeaconProximity == .far) || (discoveredbeaconProximity == .near) || (discoveredbeaconProximity == .immediate)) {
let alert = UIAlertController(title: "Alert", message: "You are in the Beacon Region", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
self.window?.rootViewController?.present(alert, animated: true, completion: nil)
}
if discoveredbeaconProximity == .unknown{
let alert = UIAlertController(title: "Alert", message: "You are out of the Beacon Region!!!", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
self.window?.rootViewController?.present(alert, animated: true, completion: nil)
}
}
注意:- 您需要更改您的信标UUID and Major and Minor
价值
因此