ビーコンは、両方のフォアグラウンドで検出されただけでなく、ときに、背景にある
ステップすることができます:あなたのビーコンがONになっているとあなたがiBeaconフレームをお楽しみくださいテストしていることを確認します。 plist
info.plistでは、「使用方法の説明」を変更する必要があります。 の場合は、NSLocationAlwaysUsageDescription
に文字列型のメッセージを追加してください。
ステップ: - バックグラウンドモード
2は、App代理人に次のコードを追加します。
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
とUserNotifications
ステップ: - の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)
}
}
注意以下のコードを追加します。フォアグラウンドまたはアクティブモード
の場合3: - あなたはそれに応じ
ビーコンごビーコン
UUID and Major and Minor
値 変更する必要がありますデバイスですか? iBeaconまたはEstimote? –iBeacon @Sharpkits –
もっとコードを表示できますか? 'locationManager.notifier = ...'と 'didRangeBeacons'コールバックメソッドの定義を設定する部分です。あなたがまだしていない場合、これらを持っている必要があります。 – davidgyoung