0
0
iOS Swiftmobile~20 mins

Location services (Core Location) in iOS Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Core Location Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
What 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)")
    }
  }
}
APrints an error message because delegate is not set.
BPrints the initial authorization status only.
CPrints nothing because startUpdatingLocation() is not called.
DPrints the latest latitude and longitude coordinates when location updates arrive.
Attempts:
2 left
💡 Hint
Check what happens inside the didUpdateLocations delegate method.
🧠 Conceptual
intermediate
1:30remaining
What 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?
ANSLocationWhenInUseUsageDescription
BNSLocationAlwaysUsageDescription
CNSLocationTemporaryUsageDescription
DNSLocationBackgroundUsageDescription
Attempts:
2 left
💡 Hint
Think about the permission that allows location only during app use.
lifecycle
advanced
1:30remaining
What 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()
ALocation updates will continue indefinitely despite stop call.
BNo location updates will be received because updates stop immediately.
CApp crashes due to invalid state transition.
DLocation updates start but only one update is received.
Attempts:
2 left
💡 Hint
Consider the effect of stopping updates right after starting.
🔧 Debug
advanced
2:00remaining
Why 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")
  }
}
AThe delegate property is not set, so delegate methods are not called.
BAuthorization request is missing, so updates are blocked.
CstartUpdatingLocation() is called before authorization request.
DdidUpdateLocations method signature is incorrect.
Attempts:
2 left
💡 Hint
Check if the delegate is assigned properly.
navigation
expert
2:30remaining
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?
AUIApplication.shared.open(URL(string: "settings://")!)
BUIApplication.shared.open(URL(string: "app-settings://")!)
C
if let url = URL(string: UIApplication.openSettingsURLString) {
  UIApplication.shared.open(url)
}
DUIApplication.shared.open(URL(string: "prefs:root=LOCATION_SERVICES")!)
Attempts:
2 left
💡 Hint
Use the official constant for opening app settings.