iOS 中的地理位置通知

发布于 2025-01-08 22:21:44 字数 133 浏览 0 评论 0原文

我想在 iOS 中实现地理位置通知,但只是在特定的日期和时间。

仅当用户在特定日期和时间处于特定位置时,才会启动通知。

例如:用户位于里约热内卢,时间为 12 点。

有谁知道如何合并这两个条件来启动通知?

I want implement a geo location notification in iOS, but just in a specific date and time.

The notification will only be launched if the user is in a certain location and at a certain date and time.

Ex.: The user is in Rio de Janeiro and is 12 o'clock.

Does anyone know how to merge these two conditions to launch a notification?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

倾城°AllureLove 2025-01-15 22:21:44

据我所知,没有任何内置的东西——你必须自己编写逻辑代码。因此,您只需从 CoreLocation 获取地理更新,您可以创建一个计时器来为您提供时间更新,然后编写一些定期执行的逻辑,如果时间/地点与您的业务规则匹配,则触发通知。请注意,应用程序必须运行才能完成此操作。您可以从外部服务器发送推送通知,但该服务器必须知道手机在哪里,并且您的应用程序必须告诉它。

这有帮助吗?

There is nothing built in that I know of -- you will have to code the logic yourself. So you will simply get geo updates from CoreLocation, and you can create a timer to give you time updates, then write some logic that execs periodically and, if the time/place matches your business rules, fire the notification. Note that the app will have to be running to accomplish this. You could send a push notification from an external server, but that server must know where the phone is, and your app must tell it.

Does this help?

谢绝鈎搭 2025-01-15 22:21:44

似乎只要给出位置更新的先例,这就可以优雅地完成。

使用此 CoreLocation startMonitoringSignificantLocationChanges

获取更新关于位置,这些更新将能够在后台启动您的应用程序(如文档中所述)。然后,在 AppDelegate 的“application:didFinishLaunchingWithOptions”方法中,包含用于检查是否是发送通知的正确时间的逻辑(通过检查 CoreData 中的内容或其他方式)。

不需要创建一个定期检查的计时器。只需让 CoreLocation 处理事件的入口点,因为它将在后台的正确位置启动您的应用程序。

It seems that this can be done elegantly as long as location updates are given precedent.

Use this CoreLocation startMonitoringSignificantLocationChanges

To get updates about location, and these updates will be able to start your app in the background (as explained in the docs). Then, in the "application:didFinishLaunchingWithOptions" method of your AppDelegate, include your logic for checking if it is the right time to send a notification (by checking with stuff in CoreData or otherwise).

There shouldn't be a need to create a timer with periodic checks. Just let CoreLocation handle the event's entry point since it'll launch your app in the background at the right location.

岁月如刀 2025-01-15 22:21:44

这很简单。我假设您熟悉使用 CLLocationManagerMKReverseGeocoder 类。出于您的目的,仅监视重大位置变化可能就可以了(即使有时仅发生在移动公里数时)。它将帮助您节省设备的电池电量。

因此,对于 CLLocationManager 的委托,您可以使用一个名为 locationManager:didUpdateToLocation:fromLocation 的方法。您需要做的就是在此处使用反向地理编码,根据使用 MKReverseGeocoder 类的实际位置来确定实际城市的名称。此外,您还必须检查设备上的本地时间,将两者匹配,然后采取相应的操作(例如,设置本地通知以从后台唤醒应用程序)。

This is simple. I assume that you are familiar using the CLLocationManager and the MKReverseGeocoder classes. For your purpose monitoring for only significant location changes would probably be fine (even if that sometimes only happens for moving kilometers). It will help you save battery power on the device.

So, for CLLocationManager's delegate there is a method called locationManager:didUpdateToLocation:fromLocation what you can use. All you need to do is to use reverse geocoding here to determine the actual city's name depending the actual location using the MKReverseGeocoder class. Also, you have to check the local time on the device, match the two, and act accordingly (set up a local notification to wake the app from the background for example).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文