抱歉延迟回复。几个月前我遇到了同样的问题,但没有找到答案:(,我只是愿意分享我的发现
您可以使用cordova 插件只担心android 设备。对于 iOS,您可以使用信标监控 api。
你只需要在xCode上导入cordoba项目
您会在项目中找到一个名为 AppDelegate.swift 的文件
在类初始化之前放这个
import CoreLocation
在类初始化之后,你需要像这样放置变量
class AppDelegate: UIResponder, UIApplicationDelegate {
var beaconManager:CLLocationManager!
var region:CLBeaconRegion!
现在按 command + N 或转到文件 > 新建 > 文件
创建一个新的 swift 文件,点击下一步并给它命名
用这个改变文件内容
import Foundation
import CoreLocation
import UserNotifications
extension AppDelegate:CLLocationManagerDelegate{
//here we will initialize our beacon monitoring manager,
//we will call this function later on the AppDelegate Main class
func initBeaconManager(){
//remember whe have defined our locationManager and region variable on AppDelegate Main Class
locationManager: CLLocationManager = CLLocationManager()
locationManager.delegate = self
//you will need to change these variables according to your becon configuration
let uuid = UUID(uuidString: "CHANGE ME YOUR BEACON UUID IF YOU DONT CHANGE IT IT WILL CRASH")!
let major = 123 //beacon major
let minor = 012 // beacon minor
let identifier = "some indentifier of your region that you like"
//you can define your region with just the uuid that way if you have more that one beacon it will work with just one region
//uncomment below if that´s the case
//let region = CLBeaconRegion(proximityUUID: uuid, identifier: identifier)
//in this case we will be ussing all variables major minor and uuid
region = CLBeaconRegion(proximityUUID: uuid, major: CLBeaconMajorValue(major), minor: CLBeaconMinorValue(minor), identifier: identifier)
//great now that we have defined our region we will start monitoring on that region
//first of all we make sure we have the correct permissions
let authorization = CLLocationManager.authorizationStatus()
switch authorization {
case .notDetermined:
//in case it is not authorized yet we will ask here for authorization
//the key is to request always authorization that way the enter and exit region callbacks will be called even if the app is killed or the phone restarted
locationManager.requestAlwaysAuthorization()
//after requesting for ahtorization we will get the authorization changed callback, there will be starting monitoring our region
case .denied, .restricted ,.authorizedWhenInUse:
//the user has denied the authorization we can make the user go to settings and enable it again
//but you can do that also ussing cordoba so... I just put in this case switch to commit that :D
break
case .authorizedAlways:
//we are goot to go now we can start monitoring
locationManager.startMonitoring(for: region)
}
if let uuid = UUID(uuidString: "B9407F30-F5F8-466E-AFF9-25556B57FE6D") {
let beaconRegion = CLBeaconRegion(
proximityUUID: uuid,
major: 100,
minor: 50,
identifier: "iBeacon")
locationManager.startMonitoring(for: beaconRegion)
}
}
//LOCATOIN MANAGER EXIT AND ENTER REGION
func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {
//notification when enter
let notificationTitle = "CHANGE ME"
let notificationBody = "CHANGE ME ENTER REGION"
sendNotification(title: notificationTitle, body: notificationBody)
}
func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion) {
//notification when leave
let notificationTitle = "CHANGE ME"
let notificationBody = "CHANGE ME EXIT REGION"
sendNotification(title: notificationTitle, body: notificationBody)
}
//Simple notification with only a custom title and body
func sendNotification(title:String,body:String){
if #available(iOS 10.0, *) {
let content = UNMutableNotificationContent()
content.title = title
content.body = body
content.sound = UNNotificationSound.default()
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 0.1, repeats: false)
let request = UNNotificationRequest(identifier: "enterNotification", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: { (error) in
// Handle error
})
} else {
let notification = UILocalNotification()
if #available(iOS 8.2, *) {
notification.alertTitle = title
}
notification.alertBody = body
notification.soundName = UILocalNotificationDefaultSoundName
UIApplication.shared.presentLocalNotificationNow(notification)
}
}
}
在文件注释上可以找到需要修改的地方
现在返回 AppDelegate 文件并将其放入函数中
func application(_ application: UIApplication, didFinishLaunchingWithOptions ....
这将初始化您在扩展上编写的所有功能
self.initBeaconManager()