0

When the app is in the foreground, the location updates every second and works perfectly, which is what I want. But when the app is in the background (or in suspended state, I'm not sure), I do not get any local notifications(Ive tested the local notifications on the phone alone, so this isn't the problem). I use local notifications to tell me if the location has been updated, which is how I know the location isn't being updated. I've set up the background capabilities and info.plist i.e "Privacy - Location When in Use Usage Description" etc. And I've added the below commands. All my code is in view controllers, not appdelegate, is this my problem? Note: this works in the background on simulator and on a real device when the device is linked to a laptop with Xcode, and the app is launched from Xcode. It does not work on a real device by itself.

class ViewController: UIViewController, CLLocationManagerDelegate{

let manager = CLLocationManager()
@IBOutlet weak var map: MKMapView!


func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let location = locations[0]
    let span:MKCoordinateSpan = MKCoordinateSpanMake(0.01,0.01) //shows the size of map screen
    let myLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude,location.coordinate.longitude)
    let region:MKCoordinateRegion = MKCoordinateRegionMake(myLocation, span)
    map.setRegion(region, animated: true)
    self.map.showsUserLocation = true
    print("Location Updated")
}





override func viewDidLoad() {
super.viewDidLoad()
manager.desiredAccuracy = kCLLocationAccuracyBest        
manager.requestWhenInUseAuthorization()
manager.requestAlwaysAuthorization()
manager.startUpdatingLocation()
manager.delegate = self
manager.allowsBackgroundLocationUpdates = true
manager.pausesLocationUpdatesAutomatically = false
         }
}

Can someone please help? This is one of the last issues I need to solve for one of my projects. Any links or working Github projects would be greatly appreciated! I just cant figure this one out!

Dan.code
  • 431
  • 2
  • 14
  • Possible duplicate of [How do I get a background location update every n minutes in my iOS application?](https://stackoverflow.com/questions/6347503/how-do-i-get-a-background-location-update-every-n-minutes-in-my-ios-application) – sschale Jul 23 '17 at 21:56
  • You should request always *or* when in use authorisation, not both. Based on the settings in your code, the rate of location update delivery is based on how fast the user is moving (ie their location is changing), not on whether you are in the foreground or background. What are you doing in your `didUpdateLocations` delegate method? Also, you should move your location code to its own class that is instantiated by the app delegate rather than relying on a specific view controller being loaded – Paulw11 Jul 23 '17 at 21:57
  • Oh I see, I was under the opinion the user had a choice on which one they wanted to use. Thank you. Im new to swift, still trying to understand the app delegate. The app delegate doesn't have any code, besides requesting local notification request. but I have set location manager as a delegate in ViewDidLoad, i.e. `manager.delegate = self` – Dan.code Jul 23 '17 at 22:23
  • A new feature in iOS 11 is the ability for the user to respond to an "always" request with a "only when in use" permission; but the app still asks for "always" or "when in use" based on the level of access that it needs. How do you know that the location is only updating every minute in the background? You can edit your question to show your current location delegate code regardless of where it is located. – Paulw11 Jul 24 '17 at 00:07
  • Ive edited the question, showed the code, and described the problem better. – Dan.code Jul 24 '17 at 00:41
  • You are updating the map view in your delegate method. This will probably cause your issue in the background. Try wrapping an if around the map view update to check if the application is currently in the background, but really your location update delegate should be some other object. You can also have a map view update itself by setting the `userTracking` property – Paulw11 Jul 24 '17 at 04:09

0 Answers0