0

I use this example: http://www.raywenderlich.com/92428/background-modes-ios-swift-tutorial

i want get user location in background every two minutes on Swift

My modification code:

class LocationViewController: UIViewController, CLLocationManagerDelegate {
  var locations = [MKPointAnnotation]()

  lazy var locationManager: CLLocationManager! = {
    let manager = CLLocationManager()
    manager.desiredAccuracy = kCLLocationAccuracyBest
    manager.delegate = self
    manager.requestAlwaysAuthorization()
    return manager
    }()

  @IBOutlet weak var mapView: MKMapView!

  @IBAction func enabledChanged(sender: UISwitch) {
    if sender.on {
        var timer = NSTimer.scheduledTimerWithTimeInterval(120, target: self, selector: Selector("test"), userInfo: nil, repeats: true)
    } else {
      locationManager.stopUpdatingLocation()
    }
  }

  @IBAction func accuracyChanged(sender: UISegmentedControl) {
    let accuracyValues = [
      kCLLocationAccuracyBestForNavigation,
      kCLLocationAccuracyBest,
      kCLLocationAccuracyNearestTenMeters,
      kCLLocationAccuracyHundredMeters,
      kCLLocationAccuracyKilometer,
      kCLLocationAccuracyThreeKilometers]

    locationManager.desiredAccuracy = accuracyValues[sender.selectedSegmentIndex];
  }


    func test(){
        locationManager.startUpdatingLocation()
    }

  // MARK: - CLLocationManagerDelegate

  func locationManager(manager: CLLocationManager!, didUpdateToLocation newLocation: CLLocation!, fromLocation oldLocation: CLLocation!) {
    // Add another annotation to the map.
    let annotation = MKPointAnnotation()
    annotation.coordinate = newLocation.coordinate

    // Also add to our map so we can remove old values later
    locations.append(annotation)

    // Remove values if the array is too big
    while locations.count > 100 {
      let annotationToRemove = locations.first!
      locations.removeAtIndex(0)

      // Also remove from the map
      mapView.removeAnnotation(annotationToRemove)
    }

    if UIApplication.sharedApplication().applicationState == .Active {
      mapView.showAnnotations(locations, animated: true)
    } else {
        locationManager.stopUpdatingLocation()
      NSLog("App is backgrounded. New location is %@", newLocation)
    }
  }
}

add timer

 @IBAction func enabledChanged(sender: UISwitch) {
    if sender.on {
        var timer = NSTimer.scheduledTimerWithTimeInterval(120, target: self, selector: Selector("test"), userInfo: nil, repeats: true)
    } else {
      locationManager.stopUpdatingLocation()
    }
  }

add "test" func

func test(){
  locationManager.startUpdatingLocation()
}

and in fun "locationManager" add

locationManager.stopUpdatingLocation()

on ios emulator i get location every two minutes, but on phone not working... why?

Vykintas
  • 401
  • 1
  • 8
  • 23
  • On which device it is not working? on what iOS version you are running the app? – Vizllx May 21 '15 at 12:42
  • yes, on device not working, device version 8.3 iphone 5s, and emulator 8.3 iphone 5s works fine, on phone Background App refresh on, Use Cellular Dara on and location Always – Vykintas May 21 '15 at 12:46
  • http://stackoverflow.com/questions/27742677/how-to-get-location-updates-for-ios-7-and-8-even-when-the-app-is-suspended – Vizllx May 21 '15 at 12:50
  • if i don't use timer works fine but when i all time check user location – Vykintas May 21 '15 at 13:20

0 Answers0