Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create local location based notifications in swift

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)

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)

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.

like image 282
Isaacm Avatar asked Dec 15 '22 16:12

Isaacm


1 Answers

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()

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)

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.

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

like image 156
Isaacm Avatar answered Dec 30 '22 21:12

Isaacm