Background activity

I’m having trouble managing my app when it slips into the background or the phone goes to sleep.
I have some lengthy network activity that fails (socket errors and such).
I’d like to either allow the app to continue working in the background, or to be able to force it to just stop doing everything so I could resume when in the foreground again, and in some cases I’d like to keep it awake.

I don’t know what is possible.
I do see the “Background Modes” option modes under capabilities, but I’m confused about how to use those right.

Suggestions?

it’s hard but you can get “some 30 seconds” of processing from a push notification in background mode. it did work in my tests but that’s a bout a year ago.

It basicly works like you send a push notification which has a background mode (fetch?) setting in it. This triggers your app (App.Opening) that runs for max 30 seco or so i believe.

You may want something different perhaps like keep the app running and just poll every x seconds on the sockets. You need background tasks to keep this up.

https://developer.apple.com/documentation/backgroundtasks?language=objc

Now i don’t know if xojo or maybe MBS or ioskit support such feature.

looks like no easy solution.
I’ll keep investigating from your link.

Thanks.

https://developer.apple.com/documentation/uikit/app_and_environment/scenes/preparing_your_ui_to_run_in_the_background/using_background_tasks_to_update_your_app?language=objc

Explained here.
Maybe @Jeremie_L or @Christian_Schmitz can fill in the blanks here?

Well, we could add some classes for this to the plugins if there is interest.

2 Likes

I believe that the event you are looking for is LaunchOptions. IIRC that event will fire if your running app is given a soft launch (not brought to the foreground) and you’ve got 30 seconds or so to do some processing. The amount of time seems to be dependent on the system load and if the device is in low power mode, your app may not run at all.

Also, remember that the user can change whether your app can run in the background at all, so don’t rely on it as the sole solution.

1 Like

I am not sure that we are all talking about the same thing.
Perhaps that’s because what I looking for just isn’t possible in iOS.

This has been really hard to figure out. On the hardware my app has a tendency to crash when being activated after it went to the background. I cannot replicate this on the simulator.
It seems some things are working as expected in the background and some are not, resulting in Nil object exceptions.

So hard to debug when it only occurs on the hardware and not in simulator - and I am yet to be able to get this project to debug on my device.

So maybe you need to be using the App.Deactivating and Screen.Deactivating things to stop sockets an timers ?

I am using the app.deactivating.
Maybe I’m missing something though. I need to spend a time finding out what is breaking.

Can you add something to Unhandled exception and push the stack to some logging system, we use airtable for this.

That’s the alarming point. I am handing unhandled exceptions, and it’s still bombing.
I’ll find it!

@Chris_Halford - you should be able to get a crash log for your app using Xcode. That’d tell you what the crash is actually about.

1 Like

I can see it looks like there is a nil object occurring, but I can’t make out what it is.
I populated the unhandled exception event in the app, and that’s not catching it.

I’ll keep digging.

If this already added?

Did anyone realize a background task in Xojo iOS App ?

Not the same thing as what the OP was posting about, but you can get location info in the background (with the users permission, of course). One of my apps uses background location to continue collecting GPS tracks when the screen is off or when the users switches to another app. In Xojo, one needs to set: MobileLocation.AuthorizationStates.AuthorizedAlways and make sure that background modes Location is checked. The background modes capabilities lists other cases but I don’t know if Xojo is wired for those. Maybe with a declare?

OK thanks - I dit test your advise. In fact that did work, so that there was some background activity in the App. Unfortunately this did not happen in times the iPhone was not moved around. For my Project i need a reliable possibility to do some small activity like actualizing the app bade and keeping pending notifications actual. With this workaround this is not sure if the iPhone is not moving. And running a Localization service in background is consuming battery.

I spent time trying to work with declares - but it seems to me that I am not familiar enough with this :frowning:

Maybe worth a feature request:
Could Xojo implement a BGAppRefreshTask as described in BGAppRefreshTask | Apple Developer Documentation a launch an App.BGAppRefreshTask event ? So dummies like me don’t have to cope with underlying declares?

Otherwise I woud appreciate any help for enabling a reliable background activity.

Hi!

There is an existing issue for this that anyone interested can upvote: https://tracker.xojo.com/xojoinc/xojo/-/issues/38573

Hi - i have found this issue and placed my request.

Hi

Dies Annonce habe a Running iOS App ritten in XOJO which makes User of background refresh tasks?