Member-only story

Get the Users Location with Core Location in Swift

Spencer Belton
1 min readSep 17, 2021

--

Technology has brought us all together and connected us in different ways. A lot of this software requires the users location. From maps services to ride share apps, current location is a key feature in todays society. Thankfully, the implementation is a nice and easy five steps.

In your Xcode project, in the file you wish to access the users location, type the following:

  1. import CoreLocation
  2. let locationManager = CLLocationManager()

Then, navigate to the ‘viewDidLoad’ function and trigger a permission request:

3. locationManager.requestWhenInUseAuthentication()

At this point, it would throw an error if you tried to run on a device or simulator. The reason is because we can’t just access the users location without there permission.

4. So we must do this by going to the Info.plist file. Then add a new property called ‘Privacy-Location When In Usage Description’. With the value of type String, type a message like “We need your location to display the current weather in your area”.

Back in ‘viewDidLoad’, and anywhere else you wish to call for the current location, type:

5. locationManager.requestLocation()

Awesome! You can fetch the users current location! Now use the protocol ‘CLLocationManagerDelegate’ and its two required functions to either handle errors or to use the current location data in your app!

--

--

Spencer Belton
Spencer Belton

Written by Spencer Belton

0 Followers

iOS App Developer

No responses yet

Write a response