【发布时间】:2016-12-01 16:34:45
【问题描述】:
我创建了一个简单的基于地理围栏的应用程序来监控人们进入和退出地理围栏的移动,我正在尝试发送有关这些事件的通知,但似乎无法正确实施。
我非常感谢一些解释性的示例代码,这些示例代码将包含在视图控制器和应用程序委托中。
P.s 我对 swift 的了解有些有限,但我了解此应用程序所需的大部分方面。 感谢您的帮助!
编辑:
这是我创建通知的功能,我认为是正确的。
func scheduleNotification() {
let centre = CLLocationCoordinate2DMake(51.364730, -0.189986)
let region = CLCircularRegion(center: centre, radius: 150, identifier: "SGS")
region.notifyOnEntry = true
let trigger = UNLocationNotificationTrigger(region: region, repeats: true)
let enterContent = UNMutableNotificationContent()
enterContent.title = "Enter"
enterContent.body = "Entered premesis"
enterContent.sound = UNNotificationSound.default()
let enterRequest = UNNotificationRequest(identifier: "enterNotification", content: enterContent, trigger: trigger)
UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
UNUserNotificationCenter.current().add(enterRequest) {(error) in
if let error = error {
print("Error: \(error)")
}
}
}
我将此添加到 didFinishLaunchingWithOptions:
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) {(accepted, error) in
if !accepted {
print("Notification access denied.")
}
}
我知道我需要调用该函数,但我不知道在哪里执行它以便在离开地理围栏时调用它。另外我不确定如何从视图控制器调用它,因为它在 AppDelegate 中。
对不起,如果我是愚蠢的,但感谢您的帮助!
【问题讨论】:
-
请发布您尝试过的和无效的。
标签: ios swift ios10 geofencing