Moh*_*afa 1 iphone ios ibeacon swift
我是快速编程的新手,我正在尝试开发app检测信标,但我仍然无法检测到任何信标.
let region = CLBeaconRegion(proximityUUID: NSUUID(UUIDString: "11111111-1111-1111-1111-111111111111")!,major: 1, minor: 1, identifier: "MyBeacon")
locationManager.startMonitoringForRegion(region)
locationManager.startRangingBeaconsInRegion(region)
Run Code Online (Sandbox Code Playgroud)
小智 5
快速 3:
首先你应该添加CoreLocation.Framework
在 .Plist 文件中添加NSLocationAlwaysUsageDescription具有适当字符串的键/字符串
在您的对象中添加 CLLocationManagerDelegate
CLLocationManagerDelegate在这个例子中添加 委托方法我将只添加 didRangeBeacons 方法
Run Code Online (Sandbox Code Playgroud)func locationManager(_ manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], in region: CLBeaconRegion) { print(beacons) }
创建并初始化 locationManager
var locationManager : CLLocationManager = CLLocationManager()
创建 CLBeaconRegion
let beaconRegion : CLBeaconRegion = CLBeaconRegion(
proximityUUID: NSUUID.init(uuidString:"****-****-****-****-******") as! UUID,
identifier: "my beacon")
Run Code Online (Sandbox Code Playgroud)将委托添加到您的对象 locationManager.delegate = self
locationManager.requestAlwaysAuthorization()
现在让我们开始范围
locationManager.startRangingBeacons(in: beaconRegion)
Run Code Online (Sandbox Code Playgroud)
这将自动调用委托方法
func locationManager(_ manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], in region: CLBeaconRegion) {
print(beacons)
}
Run Code Online (Sandbox Code Playgroud)
注意:如果你想监控信标的进入/退出状态,你需要添加
locationManager.startMonitoring(for: beaconRegion)
Run Code Online (Sandbox Code Playgroud)
最后:确保您的信标已打开并且您正在测试 iBeacon Frame 享受 :D
可以在前景和背景中检测信标
步骤: - 1.更改info.plist
在info.plist中,您需要更改"使用说明".为此,添加NSLocationAlwaysUsageDescription带有Message的String类型.
步骤: - 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)
}
Run Code Online (Sandbox Code Playgroud)
也不要忘记添加
CLLocationManagerDelegate和导入CoreLocations和UserNotifications
步骤: - 3用于ForeGround或活动模式
在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)
}
}
Run Code Online (Sandbox Code Playgroud)
注意: - 您需要相应地更改您的信标
UUID and Major and Minor值