Ios 如何实现geofence触发的本地通知?

Ios 如何实现geofence触发的本地通知?,ios,swift,ios10,geofencing,Ios,Swift,Ios10,Geofencing,我已经创建了一个简单的基于geofence的应用程序来监控人们进出geofence的移动,我正在尝试发送关于这些事件的通知,但似乎无法正确实现 我非常感谢一些解释过的示例代码,这些代码将放在视图控制器和应用程序委托中 注:我对swift的了解有限,但我了解该应用程序所需的大部分方面。 谢谢你的帮助 编辑: 这是我创建通知的功能,我认为这是正确的 func scheduleNotification() { let centre = CLLocationCoordinate2DMake(51

我已经创建了一个简单的基于geofence的应用程序来监控人们进出geofence的移动,我正在尝试发送关于这些事件的通知,但似乎无法正确实现

我非常感谢一些解释过的示例代码,这些代码将放在视图控制器和应用程序委托中

注:我对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.")
        }
    }
我知道我需要调用该函数,但我不知道在何处调用,以便在离开geofence时调用它。我也不知道如何从视图控制器调用它,因为它在AppDelegate中


对不起,如果我是愚蠢的,但谢谢你的帮助

您需要实现位置管理器和委托方法。在这些方法的实现中,您将发布本地通知

如果您已经这样做了,请确保应用程序具有发布通知的适当能力,并且您已经在应用程序代理中为应用程序注册了适当的通知设置


Ray Wenderlich在本地和远程推送通知方面有很好的和很好的区别。

请发布您尝试过的内容和不起作用的内容。因此,我不应该使用额外的功能,而应该将代码放在didEnterRegion/didExitRegion?对。当位置管理器注意到设备正在进入或退出其中一个设置的区域时,会自动调用didEnterRegion和didExitRegion。因为这是您想要发送本地通知的时候,所以从那里创建并发送它们是有意义的。