I have an iOS app which includes timer functionality. App works completely fine when in foreground and timer works good. but when i go in background mode timer works for only 30 seconds AND “NO BACKGROUND TASK EXIST WITH IDENTIFIER ERROR ” comes. Background modes & background processing capabilities are on in my app Any have solution for this??
**:SOLUTION WORKING:**
On Location Updates in Background Modes from capabilities
Put below code in your viewcontroller where timer is running
var locationManager = CLLocationManager()
CLLocationManagerDelegate
if (CLLocationManager.locationServicesEnabled())
{
self.locationManager = CLLocationManager()
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.requestAlwaysAuthorization()
self.locationManager.startUpdatingLocation()
self.locationManager.allowsBackgroundLocationUpdates = true
self.locationManager.pausesLocationUpdatesAutomatically = false
}
backgroundTaskIdentifier = UIApplication.shared.beginBackgroundTask(expirationHandler: {
UIApplication.shared.endBackgroundTask(self.backgroundTaskIdentifier!)
})
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations.last! as CLLocation
let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
_ = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))
}