Find computer location
Sometimes you may work on a project for macOS where it would be useful to know the location of the computer. You can use CoreLocation classes in MBS Xojo Plugins to query the location, but maybe it is not available, so lets check status.
We can ask locationServicesAvailable to check whether the API is available and use locationServicesEnabled to check whether it is enabled. Nowadays most Macs have location enabled, but sometimes in a cooperate environment, it may be turned off. If turned off, we can stop bothering asking the user.
You may have noticed, that we subclassed CLLocationManagerMBS as MyLocationManager and now we create an object of that class. The subclass is to be able to add the events. Once we have a new object and set the desired accuracy. In general it may be easier and quicker to get an idea on where you are. Not every software needs to know exact location, except maybe navigation software.
Let's fill in the didFailWithError event. This event fires when an error happens. You may see various error codes. This may include error code 1 for denied access. But we often see no error reported, but also no location. The application needs to be code signed and allowed by user to get the location.
Once a location is there, we get the didUpdate event called. There we can inspect the new location and output what information is comes with. In our example project, we output the values to a listbox to display them:
You may need to build the application to make it work. Please have an Info.plist file i the project with the text message for the NSLocationUsageDescription key. This text gets shown in the dialog to the user about whether to allow the location access.
Please try and see if the current position can help.