1

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))
        }
Android Devs
  • 125
  • 13
  • 4
    You cannot continue execution in background perpetually unless your app is approved for a vary narrow list of background services (VOIP, music, navigation, etc.). If you tell us what’s you’re trying to do when this timer fires, we might be able to advise you if (and if so, how) you might to achieve the desired end result. But in general, no, you can’t just keep apps running in the background perpetually. – Rob Jan 25 '20 at 07:21
  • Actually my app has a functionality in which user can set timer as per his requirements and after this duration complete i am sending local notification with sound like alarm. So that user will get back to my app to check his status if he is in background mode or using other app.. but timer in background not working in iOS 13 – Android Devs Jan 25 '20 at 09:01
  • You don't need to use a timer for that; simply schedule the local notification for the appropriate time when your app enters the background. – Paulw11 Jan 25 '20 at 11:42
  • I done that already and notification occurs at correct time but in my app there is a screen which shows timer running and in background mode it is not updating after 30 seconds – Android Devs Jan 25 '20 at 12:14
  • You say you have some screen that shows the timer when it’s in background mode. I don’t know what to make of that. What screen are you looking at when the app is backgrounded? – Rob Jan 25 '20 at 15:41
  • same issue with me, background timer does not work after 30 seconds – shaqir saiyed Mar 11 '20 at 12:28
  • @shaqirsaiyed Please check below code: On Location Updates in Background Modes from capabilities Put below code in your viewcontroller where timer is running var locationManager = CLLocationManager() CLLocationManagerDelegate – Android Devs Mar 12 '20 at 09:05
  • 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!)}) – Android Devs Mar 12 '20 at 09:07
  • @AndroidDevs Thanks, is this working for you ? Can you please provide me any reference links for this if you are having it !!? – shaqir saiyed Mar 13 '20 at 06:51

0 Answers0