In this session, I will explain in depth how the iPhone gets your location and which APIs are available in the latest version of iOS.
Do you know that the iPhone combines three different radio signals to obtain your location?
Do you know how to track the location even when your app is not running?
Do you want to get an alert when the user arrives to one specific location?
Even if your app is killed or is not active?
Do you know how to debug your location code with the iOS simulator?
Come to the session and you’ll get the answers.
This session will be a presentation with some slides and some short demos with Xcode. No prior knowledge is necessary. Everyone is welcomed.
2. Apps using
location based information
• Using location-based
information in your app is a
great way to keep the user
connected to the surrounding
world. Whether you use this
information for practical
purposes (such as navigation)
or for entertainment, location-
based information can
enhance the overall user
experience.
3. Location based information
• Location based information consists of two pieces:
location services and maps
• Location services are provided by the Core Location
framework, which defines interfaces for obtaining
information about the user’s location and heading
(the direction in which a device is pointing).
• Maps are provided by the Map Kit framework, which
supports both the display and annotation of maps
similar to those found in the Maps app.
4. How the iPhone
determines your location
• The iPhone uses a Hybrid Positioning System to
determine your location.
• This system uses a combination of GPS, a crowd-
sourced location database of nearby Wi-Fi
hotspots and the cellular triangulation technique
that determines your rough location based on cell
tower signal strength.
6. How to get
the user’s location
• Standard Location Service
✴ highly configurable way to get the current location and track
changes
• Significant-change Location Service
✴ get the current location and be notified when significant
changes occur (> 500 m)
• Region monitoring (aka geofences)
✴ boundary crossings for defined geographical regions and
Bluetooth low-energy beacon regions
7. How to get
the user’s location
• CLLocationManager
• CLLocationManagerDelegate
var locationManager: CLLocationManager?
locationManager = CLLocationManager()
locationManager?.delegate = self
locationManager?.startUpdatingLocation()
locationManager?.startMonitoringSignificantLocationChanges()
func locationManager(manager: CLLocationManager, didUpdateLocations
locations: [CLLocation])
func locationManager(manager: CLLocationManager, didFailWithError error:
NSError)
func locationManager(manager: CLLocationManager,
didChangeAuthorizationStatus status: CLAuthorizationStatus)
8. Location services
permissions
• When In Use Authorization
✴ Used when the app is in the foreground or
suspended
• Always Authorization
✴ Used always
✴ Launches the app for region monitoring or
significant location changes
9. Location services
permissions
• When In Use Authorization
var locationManager: CLLocationManager?
locationManager = CLLocationManager()
locationManager?.delegate = self
locationManager?.requestWhenInUseAuthorization()
11. Getting location information
in the background
• Enable capability
• Enable property in CLLocationManager (iOS 9+)
locationManager?.allowsBackgroundLocationUpdates = true
12. Getting location information
in the background
• When do we need to enable background location
updates in the app capabilities?
13. Getting location information
in the background
• Check if the user has not disabled background
app refresh globally or for the app
switch UIApplication.sharedApplication().backgroundRefreshStatus {
case .Available:
print("Background updates are available for the app.")
case .Denied:
print("The user explicitly disabled background behavior for this app or for the
whole system.”)
case .Restricted:
print("Background updates are unavailable and the user cannot enable them again.
For example, this status can occur when parental controls are in effect for the
current user.")
}
14. App launched
by location events
• When an app is relaunched because of a location update, the
launch options dictionary passed to your
application:willFinishLaunchingWithOptions: or
application:didFinishLaunchingWithOptions: method contains
the UIApplicationLaunchOptionsLocationKey key.
• The presence of that key signals that new location data is
waiting to be delivered to your app.
• To obtain that data, you must create a new CLLocationManager
object and restart the location services that you had running
prior to your app’s termination. When you restart those services,
the location manager delivers all pending location updates to its
delegate.
19. Region monitoring
• Two types of regions in iOS: CLCircularRegion,
CLBeaconRegion
• An app can register up to 20 regions at a time. In
order to report region changes in a timely manner,
the region monitoring service requires network
connectivity.
if CLLocationManager.isMonitoringAvailableForClass(CLCircularRegion) {
let center = CLLocationCoordinate2D(latitude: 27.968056, longitude: -82.476389)
let radius = 50.0 * 1.60934 * 1000;
let identifier = "Tampa and vicinity"
let region = CLCircularRegion(center: center, radius: radius, identifier: identifier)
region.notifyOnEntry = true
region.notifyOnExit = true
locationManager?.startMonitoringForRegion(region)
}
21. Getting the heading
and course of a device
• Core Location supports two different ways to get
direction-related information:
✴ Devices with a magnetometer can report the
direction in which a device is pointing, also
known as its heading.
✴ Devices with GPS hardware can report the
direction in which a device is moving, also
known as its course.
23. Geocoding location data
• Location data is usually returned as a pair of numerical values
that represent the latitude and longitude of the corresponding
point on the globe.
• These coordinates offer a precise and easy way to specify
location data in your code but they aren’t very intuitive for users.
• Instead of global coordinates, users are more likely to understand
a location that is specified using information they are more
familiar with such as street, city, state, and country information.
• For situations where you want to display a user friendly version of
a location, you can use a geocoder object to get that information.
24. Geocoding location data
• A geocoder object uses a network service to convert
between latitude and longitude values and a user-
friendly placemark, which is a collection of data such
as the street, city, state, and country information.
• Forward geocoding is the process of converting
place name information into latitude and longitude
values.
• Reverse geocoding is the process of converting a
latitude and longitude into a placemark
25. Geocoding location data
• Forward geocoding
var geocoder: CLGeocoder?
let adressString = "500 East Kennedy Boulevard, Tampa, FL"
geocoder = CLGeocoder()
geocoder?.geocodeAddressString(adressString, completionHandler: { (placemarks, error) in
if let placemarks = placemarks {
for placemark in placemarks {
guard
let latitude = placemark.location?.coordinate.latitude,
let longitude = placemark.location?.coordinate.longitude else { continue }
print("Found placemark at: (latitude) (longitude)")
}
}
})
Found placemark at: 27.948594 -82.45609
let adressString = "Kennedy Boulevard, Tampa, FL”
…
Found placemark at: 27.9495842266901 -82.452392334991
Found placemark at: 27.9448699419791 -82.5009904807818
[CLPlacemark]?
26. Geocoding location data
• Reverse geocoding
var geocoder: CLGeocoder?
geocoder = CLGeocoder()
let location = CLLocation(latitude: 27.948594, longitude: -82.45609)
geocoder?.reverseGeocodeLocation(location, completionHandler: { (placemarks, error) in
if let placemarks = placemarks {
for placemark in placemarks {
if let addressDict = placemark.addressDictionary {
let address = ABCreateStringWithAddressDictionary(addressDict, false)
print("Formatted address:n(address)")
}
}
}
})
Formatted address:
500 E Kennedy Blvd
Tampa FL 33602
United States
27. Things I left out
• iBeacons (monitoring and turning an iOS device
into a beacon)
• Indoor positioning
• CLVisit
• Mapkit (maps, annotations, overlays, directions)
31. How the GPS works?
• The GPS system consists of three pieces. There
are the satellites that transmit the position
information, there are the ground stations that are
used to control the satellites and update the
information, and finally there is the receiver that is
built-in in the smartphone.
32. How the GPS works?
• It is the receiver that collects data from the
satellites and computes its location anywhere in
the world based on information it gets from the
satellites.
• There is a popular misconception that a GPS
receiver somehow sends information to the
satellites but this is not true, it only receives data.
34. How the GPS works?
• The goal of the system is to always provide at least 4
satellites somewhere in the visible sky. In practice there
are usually many more than this, sometimes as many as
12.
• In order to have good fix we need:
✴ Current almanac with ephemeris data for the satellites,
expected location, expected time
✴ Given this data the only thing it needs in order to
calculate a fix is the precise location of 3 (for a 2D fix) or
4 satellites (for a 3D fix).
35. How the GPS works?
• Time to initial fix: about 45 seconds, because of
the almanac with satellite ephemeris data
• However, it could take up to 5 minutes if all the
initial data is missing
• An A-GPS reduces the time to fix to a few
seconds, because it uses a data link to download
the initial data the GPS receiver needs for the fix