Dunfey · Hotel WWDC as data, est. 1983
Front desk everything
Years
Topics

2020 Accessibility & InclusionMaps & LocationBusiness & Education

WWDC20 · 14 min · Accessibility & Inclusion / Maps & Location / Business & Education

Build location-aware enterprise apps

Develop location-aware enterprise apps for your business and personalize your employee’s everyday experience. Learn how Apple built the Caffe Macs app for its on-campus cafeterias using iBeacons and Location Services and how you can apply these tools and frameworks to your own apps, while preserving employee privacy. From there, discover how you can use localization to deliver a great experience for your international employees.

Watch at developer.apple.com ↗

Transcript all transcripts

Code shown on screen · 8 snippets

Preferences: User-defined Preferred Location swift · at 3:28 ↗
// Storing the user’s preference using UserDefaults

UserDefaults.standard.set(defaultLocation.id, forKey: "defaultLocationId")

let defaultLocationId = UserDefaults.standard.integer(forKey: "defaultLocationId")
Location Services: Requesting Authorization swift · at 6:14 ↗
// Add NSLocationWhenInUseUsageDescription to your Info.plist 
// e.g. “Location is required for placing orders while using the app."

locationManager.requestWhenInUseAuthorization()

func locationManager(
    _ manager: CLLocationManager,
    didChangeAuthorization status: CLAuthorizationStatus) {
        
    switch status {
    case .restricted, .denied: 
        disableLocationFeatures()

    case .authorizedWhenInUse, .authorizedAlways: 
        enableLocationFeatures()

    case .notDetermined: // The user hasn’t chosen an authorization status
    }
}
Location Services: Determining Device Support swift · at 7:02 ↗
if CLLocationManager.isMonitoringAvailable(for: CLBeaconRegion.self) {
    // Supports region monitoring to detect beacon regions
}

if CLLocationManager.isRangingAvailable() {
    // Supports obtaining the relative distance to a nearby iBeacon device
}
Stage 1: Region Monitoring swift · at 8:54 ↗
// Stage 1: Region Monitoring

func monitorBeacons() {
    if CLLocationManager.isMonitoringAvailable(for: CLBeaconRegion.self) {

        let constraint = CLBeaconIdentityConstraint(uuid: proximityUUID)

        let beaconRegion = CLBeaconRegion(
            beaconIdentityConstraint: constraint,
            identifier: beaconID
        )
        
        self.locationManager.startMonitoring(for: beaconRegion)
    }
}
Stage 2: Beacon Ranging swift · at 9:30 ↗
// Stage 2: Beacon Ranging

func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {
    guard let region = region as? CLBeaconRegion,
        CLLocationManager.isRangingAvailable()
        else { return }
    
    let constraint = CLBeaconIdentityConstraint(uuid: region.uuid)
    manager.startRangingBeacons(satisfying: constraint)
    beaconsToRange.append(region)
}

func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion) {
    
}
Stage 2: Beacon Ranging swift · at 10:09 ↗
// Stage 2: Beacon Ranging

func locationManager(
    _ manager: CLLocationManager,
    didRangeBeacons beacons: [CLBeacon],
    in region: CLBeaconRegion) {
    
    guard let nearestBeacon = beacons.first else { return }
    let major = CLBeaconMajorValue(truncating: nearestBeacon.major)
    let minor = CLBeaconMinorValue(truncating: nearestBeacon.major)
    
    switch nearestBeacon.proximity {
    case .near, .immediate:
        displayInformation(for: major, and: minor)
        
    default:
        handleUnknownOrFarBeacon(for: major, and: minor)
    }
}
Formatting Dates swift · at 11:32 ↗
// Formatting Dates
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .medium
dateFormatter.timeStyle = .short
dateFormatter.string(from: Date())
// "Jun 25, 2020 at 9:41 AM"
Configuring the Format of Currency swift · at 12:41 ↗
// Configuring the Format of Currency
let formatter = NumberFormatter()
formatter.currencyCode = "CAD"
formatter.numberStyle = .currency
formatter.string(from: amount)
// "CA$1.00"