How to call EventKit alarm clock with coordinates in Swift

I don't know about anyone else, but EventKit seems to use very little resources and tutorials on the internet for you to ask for help.

I need to trigger an alarm when a user is within the radius of a set of coordinates, I wasn't sure about that, I was torn between local notifications and EventKit reminders.

I decided to go for eventKit as I felt I could do more with more alarms and this was the most practical way to do it, however, after not learning much about EventKit, I had some problems.

Anyway I managed to get together and create a sample project that works and fires an alert when the user leaves their current location, the only problem is that I almost want to do the exact opposite, I want to trigger an alert when the user enters a set coordinates, I'm guessing most of the code is being passed, however I seem to be stuck on one bit mostly.

// Creates an EKStructuredLocation Instance with a title of "Current Location"
    let location = EKStructuredLocation(title: "Current Location")
    // Uses the last location update extracted from the locations array to supply you're current location
    location.geoLocation = locations.last as! CLLocation


    // Location is added to a newly created alarm instance
    let alarm = EKAlarm()
    alarm.structuredLocation = location
    // This alarm is triggered when the user moves away from the location proximity
    alarm.proximity = EKAlarmProximityLeave
    stationReminder.addAlarm(alarm)

      

I am struggling to find how to set the alarm location to coordinates, not users' location.

I tried to change this

location.geoLocation = locations.last as! CLLocation

      

to

location.geoLocation = CLCircularRegion(circularRegionWithCenter: CLLocationCoordinate2D(latitude: 37.33233141, longitude: -122.03121860), radius: 50.0, identifier: "Location1")

      

but it doesn't work, I believe I am on the right track, but I am throwing this error: Cannot assign a value of type 'CLCircularRegion!' to a value of type 'CLLocation!'

I have tried many things without permission, does anyone have experience with this and know how to help?

I also assume that I will have to change the following from this

alarm.proximity = EKAlarmProximityLeave

      

to that

alarm.proximity = EKAlarmProximityEnter

      

UPDATE

I took on board some of the comments below and tried a bunch of other things to get this to work, I feel like I'm so close but something is just missing. I cannot get this alarm to go off. Sorry for all the comments on the codes, it's just that you can see some of the attempts I've made in fixing this.

can anyone see something wrong with this alarm code?

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {

    // Stops location manager from sending further updates
    locationManager.stopUpdatingLocation()

    // Creates a new EKReminder which is named and initialised with text from the UITextField
    let stationReminder = EKReminder(eventStore: appDelegate!.eventStore)
    stationReminder.title = locationText.text

    // Stores the previously created EKReminder in the default calendar
    stationReminder.calendar = appDelegate!.eventStore!.defaultCalendarForNewReminders()

    // Creates an EKStructuredLocation Instance with a title of "Current Location"
    let location = EKStructuredLocation(title: "Destination: Bournemouth Station")
    // Uses the last location update extracted from the locations array to supply you're current location
//        location.geoLocation = locations.last as! CLLocation
//        location.geoLocation = CLCircularRegion(circularRegionWithCenter: CLLocationCoordinate2D(latitude: 37.33233141, longitude: -122.03121860), radius: 50.0, identifier: "Location1")
//        location.geoLocation = CLLocation(latitude: 50.742771, longitude: -1.895072)
    location.radius = 50.0

    location.geoLocation = CLLocation(latitude:50.742771, longitude:-1.895072)
//        location.radius = 10.0 // metres

    // Location is added to a newly created alarm instance
    let alarm = EKAlarm()
    alarm.structuredLocation = location
    // This alarm is triggered when the user moves away from the location proximity
//        alarm.proximity = EKAlarmProximityEnter
    alarm.proximity = EKAlarmProximityEnter // "geofence": we alarm when *arriving*
    // but this will have no effect until Reminders is granted Location access...
    // and in iOS 8 it won't even ask for it until it is launched
    // also, in iOS 8 the separate background usage pref is withdrawn;
    // instead, auth of Reminders for "when in use" covers this...
    // ...because it means "this app *or one of its features* is visible on screen"
    stationReminder.addAlarm(alarm)

    // Now we have a fully configured reminder which we save in the Event Store
    var error: NSError?
    appDelegate!.eventStore?.saveReminder(stationReminder,
        commit: true, error: &error)

    if error != nil {
        println("Reminder failed with error \(error?.localizedDescription)")
    }
}

      

+3


source to share


1 answer


It looks like the EKEvent has EKStructuredLocation

which you are using correctly. However, you must be careful with the type of the geoLocation property. It must be a CLLocation that is not the same as CLCircularRegion.

Steps to fix:



0


source







All Articles