Location Reminders Inside my App

Hello everyone!!


To make this simple, I'm trying to create an app that uses your location, compared to a location you set, to then remind you of something. My hopes were that the location variable would be similar to a boolean variable/point, and when it was true 'this' would happen or 'this' would happen, depending on the users preference.


My question is simple, how can I retrieve the users location to then notify them of the reminder he or she set?

Or better yet, how can I retrieve the users location and it be a boolean TRUE or FALSE depending on where they are in relation to the location they set?


note: I understand this feature is available in the "Reminders" app already on the iPhone, but I'm trying to understand how I can include it into my application.

*In the app I am creating, in "Capabilities" I have "Background Modes" set to 'ON', and the 'Modes:' I've checked are 'Location updates', 'Background fetch', and 'Remote notification'


Thanks guys!

Replies

You can use code like this:

    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate=self;
    [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];



-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
    currentLocation=[locations lastObject];

   // and use the following:
   currentLocation.coordinate.latitude
    currentLocation.coordinate.longitude
// and
     distanceFromLocation


https://developer.apple.com/documentation/corelocation/cllocation/1423689-distancefromlocation?language=objc

I've been trying to work this code in for a good while now, just hoping something would click, but I'm coming up short. I've placed it after my

override func viewDidLoad() {
        super.viewDidLoad()
        /
    }

but before

override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        /
    }


...and I'm getting the error, "Expected declaration" in multiple locations. I've determined the culprit to be "locationManager" as it appears anytime it's called.


I feel like there must be a setting or some check box I failed to click somewhere because it seems as if the program doesn't understand what locationManager is...

Did you declare


import CoreLocation


at bthe beginning of the class ?

"import CoreLocation" is at the top, just under "import UIKit", and still...'Expected declaration' error...

import UIKit
import CoreLocation
class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
    }

    locationManager = [[CLLocationManager alloc] init];              //Expected declaration [error]
    locationManager.delegate=self;                                   //Expected declaration [error]
    [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];    //Expected declaration [error]



    (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{     //Expected declaration [error]
    currentLocation=[locations lastObject];

Not sure what I'm missing...

Thank you for the help!

My best guess would be you have an extra or missing closing curly brace somewhere, in the class definition.


Could you inspect thoroughly ?

It doesn't appear to be a missing, or extra, curly brace.


Here is the code in it's entirety,


import UIKit
import CoreLocation
class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
    }
   
    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate=self;
    [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
   
   
   
    (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
    currentLocation=[locations lastObject];
   
  
    currentLocation.coordinate.latitude
    currentLocation.coordinate.longitude
    
    distanceFromLocation
   
    }
   

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        
    }
}

I would really like to figure this out...


What I believe I've failed to do is,

Add the necessary usuage description keys to my information propiety list

Give my application program the necessary Authorization


Now the question is, if those are needed, how can they be done correctly? I've added an "Excuteable file" to the 'info.plist' with the Type set as String and the Value as NSLocationWhenInUseUsageDescription

In Info.plist, the key is effectively NSLocationWhenInUseUsageDescription


And just enter a text like : "My app needs to know where you are to do this or that"


You may need also NSLocationAlwaysUsageDescription


See here for details about the keys and when they are required:

h ttps://developer.apple.com/library/content/documentation/General/Reference/InfoPlistKeyReference/Articles/CocoaKeys.html#//apple_ref/doc/uid/TP40009251-SW26

Alright. I can understand that those are necessary so I have included them into my info.plist file, yet I'm still getting quite a lot of errors. Mostly the the Expected Declaration error. I've been editing it a lot and moving things around as well as putting more things in that I've researched, and that you all have reccomended. So now it doesn't appear much like what I had before. And in Info.plist, I have 'Privacy - Location When In Use Usage Descriptio'n, as well as 'Privacy - Location Always Usage Description'.

(Though my value for the 'Always Usage' is a just a String which is a reminder to myself that I shouldn't use this one because Apple reccomends against it)


My first question is, how can I make something happen when two things are true, and one of those things being proximity to a designated locaion? My hope was Boolean1 and Boolean2, and only when they are both true a Boolean3 is made true.


Can you see an error in this?

//
// Copyright © 2018 Rob. All rights reserved
//
import UIKit
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate {

    var locationManager:CLLocationManager!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        determineMyCurrentLocation()
    }


    func determineMyCurrentLocation() {
        locationManager = CLLocationManager()
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.requestAlwaysAuthorization()

        if CLLocationManager.locationServicesEnabled() {
            locationManager.startUpdatingLocation()
            // locationManager.startUpdatingHeading()
        }
    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        let userLocation:CLLocation = locations[0] as CLLocation

        // Call stopUpdatingLocation() to stop listening for location updates,
        // other wise this function will be called every time when user location changes.

        // manager.stopUpdatingLocation()

        print("user latitude = \(userLocation.coordinate.latitude)")
        print("user longitude = \(userLocation.coordinate.longitude)")
    }

    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error)
    {
        print("Error \(error)")
    }
}


Maybe I need to back WAY up and start learning again...but where can I start? If you have a reccomendation on how or where I should start taking a "class" for Xcode, let me know.