Apple/iOS/Kits/CoreLocation/Swift

Un article de Agora2ia.


Avant tout, ne pas oublier de compléter le Info.plist :

  • Voir sur stackoverflow.com
  • MyProject > MyTarget > Info >
    • Key: NSLocationWhenInUseUsageDescription
    • Type: string
    • Value: We would use your location to understand our customer and improve our product.

Puis voici le code :

 import CoreLocation
 
 class MyViewController: UIViewController, CLLocationManagerDelegate {

   let locationManager = CLLocationManager()

   override func viewDidLoad() {
       super.viewDidLoad()
       logLocationInFlurry()
   }
   
   //
   // MARK: - Core Location support
   //
   
   // Flurry Analytics - #17/6.b
   func logLocationInFlurry() {
       if CLLocationManager.locationServicesEnabled() {
           locationManager.delegate = self
           locationManager.desiredAccuracy = kCLLocationAccuracyBest
           locationManager.requestAlwaysAuthorization()
           locationManager.requestWhenInUseAuthorization() // For use in foreground
           locationManager.startUpdatingLocation()
       }
   }
   
   // Flurry Analytics - #17/6.c
   // CLLocationManagerDelegate
   func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
       var location:CLLocation = manager.location
       var coordinate:CLLocationCoordinate2D = location.coordinate
       println(">>> Location For FLurry = \(coordinate.latitude) \(coordinate.longitude)")
       Flurry.setLatitude(coordinate.latitude, longitude:coordinate.longitude, horizontalAccuracy:Float(location.horizontalAccuracy), verticalAccuracy:Float(location.verticalAccuracy))
       locationManager.stopUpdatingLocation()
   }
   
   // func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) { println("#a") }
   func locationManager(manager: CLLocationManager!, didUpdateHeading newHeading: CLHeading!) { println("#b") }
   func locationManagerShouldDisplayHeadingCalibration(manager: CLLocationManager!) -> Bool {
       println("#c")
       return true
   }
   func locationManager(manager: CLLocationManager!, didDetermineState state: CLRegionState, forRegion region: CLRegion!) { println("#d") }
   func locationManager(manager: CLLocationManager!, didRangeBeacons beacons: [AnyObject]!, inRegion region: CLBeaconRegion!) { println("#e") }
   func locationManager(manager: CLLocationManager!, rangingBeaconsDidFailForRegion region: CLBeaconRegion!, withError error: NSError!) { println("#f") }
   func locationManager(manager: CLLocationManager!, didEnterRegion region: CLRegion!) { println("#g") }
   func locationManager(manager: CLLocationManager!, didExitRegion region: CLRegion!) { println("#h") }
   func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) { println("#i - error=\(error)") }
   func locationManager(manager: CLLocationManager!, monitoringDidFailForRegion region: CLRegion!, withError error: NSError!) { println("#j") }
   func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) { println("#k : \(status.rawValue)") }
   func locationManager(manager: CLLocationManager!, didStartMonitoringForRegion region: CLRegion!) { println("#m") }
   func locationManagerDidPauseLocationUpdates(manager: CLLocationManager!) { println("#n") }
   func locationManagerDidResumeLocationUpdates(manager: CLLocationManager!) { println("#o") }
   func locationManager(manager: CLLocationManager!, didFinishDeferredUpdatesWithError error: NSError!) { println("#p") }
   func locationManager(manager: CLLocationManager!, didVisit visit: CLVisit!) { println("#q") }