在swift中创建基于位置的本地通知 [英] Create local location based notifications in swift

查看:83
本文介绍了在swift中创建基于位置的本地通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个相对较新的快速开发人员,我在iOS 8中听说您可以根据用户位置发送本地通知。我看了一些代码,特别是这个代码来创建简单的基于时间的本地通知。

I'm a relatively new swift developer and I've heard in iOS 8 you can send local notifications based on a users Location. I have had a look at some code, particularly this one to create simple time based local notifications.

var leftNotification1:UILocalNotification = UILocalNotification()
    leftNotification1.alertBody = "NotidicationA"
    leftNotification1.repeatInterval = NSCalendarUnit.CalendarUnitHour
    leftNotification1.fireDate = NSDate(timeIntervalSinceNow: 900)
    UIApplication.sharedApplication().scheduleLocalNotification(leftNotification1)

我还看到你可以用类似这样的位置触发器替换fireDate:

I have also seen that you can replace fireDate with a location trigger like something like this:

var localNotification:UILocalNotification = UILocalNotification()
    localNotification.regionTriggersOnce = true
    localNotification.region = CLCircularRegion(circularRegionWithCenter: CLLocationCoordinate2D(latitude: 37.33233141, longitude: -122.03121860), radius: 50.0, identifier: "Location1")
    UIApplication.sharedApplication().scheduleLocalNotification(localNotification)

我知道您需要获得用户许可的位置并对位置进行轮询。我不知道如何做这种事情并将其与该代码链接。当我将此代码输入我的ViewDidLoad时,就像我为顶级代码输入的那样,由于显而易见的原因,它无法正常工作,CoreLocation未注册且不对位置进行轮询。如果有人可以告诉我如何使这些代码正常工作,或者甚至更好地给我一个示例代码来看看这将是伟大的。谢谢。

I know with locations you need permission from the user and poll for locations and that. I am not aware of how to do that sort of thing and link it with that code. When I just enter this code into my ViewDidLoad like I do for the top one it doesn't work for obvious reasons, the CoreLocation isn't registered and its not polling for locations. If someone could inform me about how to get this code working or even better give me an example code to take a look at that'll be great. Thanks.

推荐答案

我发现一些指南有不同的方法来解决这个问题,你需要 import CoreLocation 然后你想要在ViewController.swift的ViewController函数中注册CLLocationManagerDelegate。

I found a few guides that had different ways of approaching this, you need to import CoreLocation then you'll want to register the CLLocationManagerDelegate in the ViewController function in ViewController.swift.

你需要获得使用许可位置

You'll need to get permission for using the location with

locationManager.delegate = self
locationManager.requestAlwaysAuthorization()
UIApplication.sharedApplication().cancelAllLocalNotifications()

如果你使用requestWhenInUseAuthorization,它将不会显示通知,因为它们只是当您不在应用程序中时显示。在iOS 8中,它要求您在info.plist文件中声明 NSLocationAlwaysUsageDescription ,您只需输入一些文本,以便在请求位置访问时显示。

If you use the requestWhenInUseAuthorization instead it won't display the notifications as they only are displayed when you are not in the app. In iOS 8 it requires you to declare NSLocationAlwaysUsageDescription in the info.plist file you'll just enter some text to appear when location access is requested.

完成所有操作后,您可以开始创建通知,您可以使用以下代码来执行此操作

Once you've done all of that you can start creating your notifications, you can use the following code to do that

let locattionnotification = UILocalNotification()
locattionnotification.alertBody = "You have entered a high risk zone (Crown Range Road) , proceed with caution"
locattionnotification.regionTriggersOnce = false
locattionnotification.region = CLCircularRegion(circularRegionWithCenter: CLLocationCoordinate2D(latitude:
    37.33182, longitude: -122.03118), radius: 100.0, identifier: "Location1")
UIApplication.sharedApplication().scheduleLocalNotification(locattionnotification)

如果在进入和退出区域时收到多条消息,则regionTriggersOnce选项允许您打开和关闭或者当您第一次进入该区域时只有一个。

The regionTriggersOnce option allows you to turn on and off if you get multiple messages when you enter and exit the zone or only one when you first enter the area.

您可以将纬度,经度和半径(以米为单位)的值更改为您想要的值以及在半径范围内的值您将收到有关消息内容的警告。

You can change the latitude, longitude and radius (in meters) values to what you want and when you get within the radius of the point you will be alerted with the contents of the message.

有关不同选项的更多详细信息,请查看 https://developer.apple.com/library/ios/documentation/iPhone/Reference/UILocalNotification_Class/

For more details about the different options have a look at https://developer.apple.com/library/ios/documentation/iPhone/Reference/UILocalNotification_Class/

这篇关于在swift中创建基于位置的本地通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆