How do I get background location running in an Expo EAS application on iOS?

Viewed 370

I have an EAS stand alone app that was built using Expo SDK 43. I am trying to request permissions to access a user's background location (location when the app is not open but running in the background)

When the app builds the console throws this warning:

TaskManager: Task "background-location-task" has been executed but looks like it is not
 defined. Please make sure that "TaskManager.defineTask" is called during initialization phase.

It also throws this warning as well:

[Unhandled promise rejection: Error: Task 'background-location-task' not found for app ID 'mainApplication'.]

I believe I am setting up background locations properly, but it appears I may not be executing something correctly. Below is my code for how I request background locations.

Note: I have of the background request in a useEffect, and the Task-Manager outside the useEffect inside the file following foreground location requests.

// app.json & Info.Plist:

"ios":{
   ...
     "UIBackgroundModes":[
       "location",
       "fetch"
      ]
 }

//Inside the main file

import * as Location from 'expo-location';
import * as TaskManager from 'expo-task-manager'
import * as BackgroundFetch from 'expo-background-fetch

const LOCATION_TASK_NAME = 'background-location-task';


useEffect(()=>{


async function getBackgroundLocationAsync(){
   let {status} = await Location.requestBackgroundPermissionsAsync()
     if(status!=='granted'){
        setErrorMsg('Permission to access background location was denied')
        return;
       }
       if(status ==='granted'){
         await Location.startLocationUpdatesAsync(LOCATION_TASK_NAME,{
           accuracy: Location.Accuracy.Balanced,
       })
     }

     getBackgroundLocationAsync()

     },
   [],
  )


TaskManager.defineTask(LOCATION_TASK_NAME,({data, error})=>{
  if(error){
 return;
}
if(data){
  const{locations}=data
 }
})
0 Answers
Related