iOS app doesn't run in background when alone

NikhileshParmarNikhileshParmar ✭✭USMember ✭✭

Hi,
seems that my ios app doesn't keep running in background when is the only app running on the iPhone.
My app get GPS changes in background through the Geolocator plugin, and if I run the music app (or youtube, or a game) while my app is in background, it works fine.
If I run my app in background and I lock my iphone with no other apps in background, it stops working after few minutes.
so is like if iOS kills my app.

I think that I have set the info.plist file correctly:

Required device capabilities (armv7)
Required background modes (App registers for location updates)
Privacy - Location always usage description
Privacy - Location always and when in use Usage description
Privacy - Location When in Use Usage Description

I'm developing my app with Xamarin.Forms.

Answers

  • LWDeSouzaLWDeSouza ✭✭ Member ✭✭

    How are you implementing the background code ?
    To achieve background outside of your app, you will need some native implementation.

  • NikhileshParmarNikhileshParmar ✭✭ USMember ✭✭

    hi @LWDeSouza
    We did not implement any background code in native. We just enable background mode in info.plist. And we just code like below in Portable.

    In Portable:

    Task.Run(() =>
    {
    try
    {
    //loopInterval is 30 seconds by default
    TimeSpan loopInterval = new TimeSpan(0, 0, 30);
    Xamarin.Forms.Device.StartTimer(loopInterval, () =>
    {
    Device.BeginInvokeOnMainThread(async () =>
    {
    if (DependencyService.Get().IsInternetConnectionAvailable() && ServiceWorksContext.IsTrackingEnable)
    {
    DependencyService.Get().StartGeoLocation();
    if (DependencyService.Get().IsGeoLocationEnable())
    {
    var position = await locator.GetPositionAsync(TimeSpan.FromSeconds(10));
    _customerSoapService.InsertTechnicianRoute(ServiceWorksContext.CurrentUser.employeeId, position.Latitude.ToString(), position.Longitude.ToString(), loginUserId.ToString());

                                                                    }
    
                                                                }
    
                                                            });
    
                                                return true;
                                            });
                }
                catch (Exception ex)
                {
    
                }
    
            });
    

    in IOS side:

    public void StartGeoLocation()
    {
    var locationManager = new CoreLocation.CLLocationManager();

            if (UIDevice.CurrentDevice.CheckSystemVersion(8, 0))
            {
                locationManager.RequestAlwaysAuthorization();
                locationManager.RequestWhenInUseAuthorization();
            }   
            if (UIDevice.CurrentDevice.CheckSystemVersion(9, 0))
            {
                //locationManager.AllowsBackgroundLocationUpdates = true;
                //locationManager.PausesLocationUpdatesAutomatically = false;
            }
        }
    
        public bool IsGeoLocationEnable()
        {
            bool retval = false;
            if (CLLocationManager.LocationServicesEnabled)
            {
                if (CLLocationManager.Status == CLAuthorizationStatus.Authorized || CLLocationManager.Status == CLAuthorizationStatus.AuthorizedAlways|| CLLocationManager.Status == CLAuthorizationStatus.AuthorizedWhenInUse)
                {
                    retval= true;
                }
            }
            return retval;
        }
    

    Still i need to code for background mode in native?

  • LWDeSouzaLWDeSouza ✭✭ Member ✭✭

    Yes, you do need extra code to keep it running in the system background.
    Task.Run() keep the thread running in the background of YOUR APP.

    Homever, if your app goes to the background, your task stops.

    To keep it running even when your app is not active, you will need to implemente some kind of messaging.

    Take a look at:
    https://robgibbens.com/backgrounding-with-xamarin-forms/

    I did it in my app, and it Works perfectly.

Sign In or Register to comment.