Challenge - 5 Problems
Core Location Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediateWhat is the output when location updates are received?
Consider this Swift code snippet using Core Location. What will be printed when the device location updates?
iOS Swift
import CoreLocation class LocationHandler: NSObject, CLLocationManagerDelegate { let manager = CLLocationManager() override init() { super.init() manager.delegate = self manager.requestWhenInUseAuthorization() manager.startUpdatingLocation() } func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { if let location = locations.last { print("Lat: \(location.coordinate.latitude), Lon: \(location.coordinate.longitude)") } } }
Attempts:
2 left
💡 Hint
Check what happens inside the didUpdateLocations delegate method.
✗ Incorrect
The delegate method didUpdateLocations is called when new location data arrives. The code prints the last location's latitude and longitude.
🧠 Conceptual
intermediateWhat permission is required to access location in foreground?
Which key must be added to Info.plist to request permission for location access only when the app is in use?
Attempts:
2 left
💡 Hint
Think about the permission that allows location only during app use.
✗ Incorrect
NSLocationWhenInUseUsageDescription is the key to request location access only when the app is active in the foreground.
❓ lifecycle
advancedWhat happens if you call stopUpdatingLocation() too early?
Given this code snippet, what is the effect of calling stopUpdatingLocation() immediately after startUpdatingLocation()?
iOS Swift
manager.startUpdatingLocation() manager.stopUpdatingLocation()
Attempts:
2 left
💡 Hint
Consider the effect of stopping updates right after starting.
✗ Incorrect
Calling stopUpdatingLocation() right after startUpdatingLocation() cancels location updates immediately, so no updates are delivered.
🔧 Debug
advancedWhy does this code not print location updates?
This code does not print location updates as expected. What is the most likely cause?
iOS Swift
class LocationManager: NSObject, CLLocationManagerDelegate { let manager = CLLocationManager() override init() { super.init() manager.requestWhenInUseAuthorization() manager.startUpdatingLocation() } func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { print("Location updated") } }
Attempts:
2 left
💡 Hint
Check if the delegate is assigned properly.
✗ Incorrect
Without setting manager.delegate = self, the delegate methods won't be called, so no location updates are printed.
expert
How to navigate to app settings for location permission?
Which code snippet correctly opens the app's settings page so the user can change location permissions?
Attempts:
2 left
💡 Hint
Use the official constant for opening app settings.
✗ Incorrect
UIApplication.openSettingsURLString is the official URL string to open the app's settings page. Other URLs are deprecated or invalid.
