-1

I am working on watch heart beat app and I want when user heart rate critical then we will get his current location in(foreground, background and terminate) and send it to our server. Is there any way through which I get user location only at that position. I don't want to update his location every time.

Nasib
  • 9
  • 3
  • Possible duplicate of [How can I get current location from user in iOS](http://stackoverflow.com/questions/4152003/how-can-i-get-current-location-from-user-in-ios) – Mihriban Minaz Apr 08 '16 at 13:00

1 Answers1

2

To get user location you have to declare :

let locationManager = CLLocationManager()

in your controller.

Then, in viewDidLoad you have to request for location and initialize the CLLocationManager get process :

// Ask for Authorisation from the User.
self.locationManager.requestAlwaysAuthorization() 

// For use in foreground
self.locationManager.requestWhenInUseAuthorization()

if CLLocationManager.locationServicesEnabled() {
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
    locationManager.startUpdatingLocation()
}

You will get location in CLLocationManagerDelegate :

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    var location:CLLocationCoordinate2D = manager.location.coordinate
    print("locations = \(location.latitude) \(location.longitude)")
}

In your info.plist you have to add NSLocationAlwaysUsageDescription and custom alert message to show while requesting for location.

cheers...

Muhammad Ali
  • 2,173
  • 15
  • 20
  • But How can I get location when my app in terminated state – Nasib Apr 08 '16 at 11:48
  • If a user terminates the app the system no longer grants it the same privileges. Background fetch operations and background location will not get executed until the user decides to start up your app again. – Muhammad Ali Apr 08 '16 at 11:54
  • Please read my description I have mention it. I know we can get current location with the help of significant location update but its not update continues. – Nasib Apr 08 '16 at 12:23