How to Make Location Access Again
Requesting location: Core Location
It should come equally no surprise that Apple considers a user'due south location to be private, and that means we need to inquire for permission to employ it. How you ask for permission depends on what you're trying to do: would you like the user'due south location only when your app is running, or would you like a user'southward location fifty-fifty when your app isn't running?
You might think that you'd only ever want location access when your app is running. After all, what'south the bespeak in request for information when your app isn't around to use it?
There are times you'll want both. For instance, if yous're creating a map app that shows users how to get from their current location to your nearest shop, y'all'll only need their location when the app is existence used. But if you're creating an app that needs to be woken upwardly when the user reaches a location, then you'll need access fifty-fifty when the app isn't running – iOS monitors the user's location on your behalf and automatically starts your app as needed.
Using location when the app isn't running is of form highly sensitive information, so Apple tree flags it upwards in three ways:
- If y'all request Always access, users will nevertheless get the chance to choose When In Use.
- If they cull Ever, iOS volition automatically ask them once more after a few days to confirm they still desire to grant Always access.
- When your app is using location data in the background the iOS UI volition update to reverberate that – users will know it's happening.
- Users can, at any point, go into the settings app and change from Ever down to When In Utilize.
In this app we're going to asking E'er access so that our app can discover beacons in the background. Requesting location access requires a change to your apps Info.plist file, which is the belongings list file nosotros outset met way dorsum in project 3. Nosotros need to add to that file the reason why we want the user'south location – a string that volition exist shown in the iOS UI when the user is beingness asked to have or decline our request.
Because of the rules above, we need to add 2 keys: "Privacy - Location Ever and When In Apply Usage Description" and "Privacy - Location When In Use Usage Clarification". And so, please add them both now, make certain their types are set up to Cord, so in the value fields enter some text to explain to users why you want their location. For case, "We want to assistance you observe your nearest store." When your user is prompted to grant location admission, this text will exist shown alongside Apple tree's own descriptive message.
That'south plenty knowledge to get this app spring started, then open up Main.storyboard and place a label in there. Give it the custom font System Thin size 40, then give the text "UNKNOWN". For constraints, please center it horizontally and vertically. At present create an outlet for it using the assistant editor, and proper noun the outlet distanceReading.
That label will testify one of four messages depending on how close we are to our test buoy, which of course might be an iPad acting as a beacon if you don't own actual hardware. Because iBeacons use very low energy levels, their range is limited and as well easily interrupted; fifty-fifty something every bit simple as turning your back to the beacon weakens its signal dramatically. Based on the beacon'south distance to us, we'll show either "UNKNOWN", "FAR", "NEAR" or "Correct HERE".
Apple restricts your ranging to these values because of the signal'due south depression energy nature, but information technology's more than enough for nearly uses.
To complete our current step, let's make sure we have location configured correctly. This bit will work fine on the simulator, considering although the simulator isn't capable of detecting iBeacons it can simulate full general location data well enough.
Open ViewController.swift and add together this import alongside UIKit:
import CoreLocation Now add this property to your class:
var locationManager: CLLocationManager? This is the Core Location class that lets united states of america configure how nosotros desire to be notified virtually location, and will as well evangelize location updates to united states of america.
That doesn't actually create a location manager, or even prompt the user for location permission! To do that, we first demand to create the object (like shooting fish in a barrel), then gear up ourselves as its consul (easy, but nosotros need to conform to the protocol), then finally nosotros need to request authorization. We'll start by befitting to the protocol, then change your course definition to this:
class ViewController: UIViewController, CLLocationManagerDelegate { Now modify your viewDidLoad() method to this:
override func viewDidLoad() { super.viewDidLoad() locationManager = CLLocationManager() locationManager?.delegate = self locationManager?.requestAlwaysAuthorization() view.backgroundColor = .gray } Creating the object and setting the consul are like shooting fish in a barrel plenty, but the requestAlwaysAuthorization() phone call is new. This is where the bodily action happens: if y'all have already been granted location permission then things will Just Work; if you lot haven't, iOS will request it now.
Note: if you lot used the "when in use" fundamental, you should call requestWhenInUseAuthorization() instead. If you did not ready the right plist key earlier, your request for location access will be ignored.
I slipped 1 other thing in there: I prepare the view'southward background colour to be gray. As well every bit changing the label'due south text, nosotros'll be using color to tell users how distant the beacon is.
Requesting location authority is a non-blocking call, which means your code will carry on executing while the user reads your location message and decides whether to grant you lot access to their location.
When the user has finally made their listen, y'all'll get told their result because nosotros set ourselves every bit the delegate for our CLLocationManager object. The method that will exist chosen is this one:
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) { if status == .authorizedAlways { if CLLocationManager.isMonitoringAvailable(for: CLBeaconRegion.cocky) { if CLLocationManager.isRangingAvailable() { // do stuff } } } } Put that into your view controller class somewhere, then run your app. It's of import to test it earlier standing, considering if you've made a mistake somewhere information technology's hard to know unless you finish and cheque. The virtually mutual error is misconfiguring the plist with location privacy settings, so if yous don't see a message requesting location admission then check there kickoff.
The didChangeAuthorization method we merely added doesn't do anything considering it just has a comment proverb // exercise stuff. We'll fill that in with great stuff shortly, but for now look at the conditional statements wrapped around it: did we get authorized by the user? If then, is our device able to monitor iBeacons? If so, is ranging available? (Ranging is the ability to tell roughly how far something else is away from our device.)
Sponsor Hacking with Swift and reach the world's largest Swift customs!
melendezcinissente.blogspot.com
Source: https://www.hackingwithswift.com/read/22/2/requesting-location-core-location
0 Response to "How to Make Location Access Again"
Postar um comentário