【发布时间】:2018-09-15 20:53:53
【问题描述】:
当检测到信标信号时,我的信标通知会随时触发。如何将其限制为每天仅接收一次通知?
我实施了日期检查,但这没有帮助。 if/else 检查工作正常,但忽略此检查会触发信标通知
非常感谢任何解决方法的建议!
这里是我的代码 sn-p:
class ViewController: UIViewController, CLLocationManagerDelegate {
let locationManager = CLLocationManager()
let date = Date()
let calendar = Calendar.current
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.requestAlwaysAuthorization()
locationManager.desiredAccuracy = 10
locationManager.distanceFilter = 100
locationManager.startUpdatingLocation()
let year = calendar.component(.year, from: date)
let month = calendar.component(.month, from: date)
let day = calendar.component(.day, from: date)
let currentDate = "\(year)\(month)\(day)"
var savedDate = UserDefaults.standard.string(forKey: "savedDate") ?? "12345"
print(currentDate)
print(savedDate)
if (savedDate == currentDate){
print("same date - no action")
} else {
savedDate = currentDate
UserDefaults.standard.set(savedDate, forKey: "savedDate")
print(savedDate + " execute program")
let beaconRegion = CLBeaconRegion(proximityUUID: UUID(uuidString: "13D9F4C7-A68D-46F4-8D35-4BA7F64BC417")!, identifier: "estimote")
beaconRegion.notifyOnEntry = true
beaconRegion.notifyOnExit = false
let content = UNMutableNotificationContent()
content.title = "???? Daily beacon check! ????"
content.subtitle = "Receive a new info every day!"
content.body = "ONLY ONCE A DAY WE SUPPLY INFO!"
content.sound = .default
content.badge = 1
let trigger = UNLocationNotificationTrigger(region: beaconRegion, repeats: true)
let identifier = "estimote"
let request = UNNotificationRequest.init(identifier: identifier, content: content, trigger: trigger)
UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
UNUserNotificationCenter.current().add(request, withCompletionHandler: { (error) in
})
self.locationManager.startRangingBeacons(in: beaconRegion)
}
}
【问题讨论】:
-
这看起来是一个非常简单的调试问题。你做了正确的事,在我看到
print(currentDate)和print(savedDate)的地方添加了两个调试语句。这些打印出来的是什么?这个问题的答案应该可以让您知道为什么您的程序无法运行。