HELP PLS -- Does Unity iOS backgrounding actually work?

I have MacBook with Monterey-12.3.1 running Unity-2021.3.1f1 and using Xcode-13.3.1
targeting an iPhone running iOS-15.4.1 — as best I can tell, the latest of everything.

I build my app, the plist sets background-refresh and background processing, with the
background specified task subitem set to be my app ‘abd’ in reverse domain ‘xyz’ (com.xyz.abc)

The app runs fine in foreground, but when swiped into background it is suspended. no processing.

Has anybody used Unity to create an app that can run on a recent iPhone in background?

Any and all pointers or hints or suggestions greatly appreciated.

Hello, iOS does not allow running full app in background, only some events can get execution time to be handled. Unity runtime is paused when app enters background, but it is possible to receive some available background events (location update, push notification, etc.) in native code and pass that to Unity to be handled in managed C# code. Here’s an example for adding support for background execution.

1 Like

@Petras-Unity : I tried that GitHub example.

  • there seems to be no way to fetch a zip of the whole thing?
  • I fetched the individual pieces (esp the plugin .mm),
    and tried to build and run BackgroundFetch onto my iPhone but got a message that "OnGUI"was stripped:
    OnGUI function detected on MonoBehaviour, but not called, because IMGUI module is stripped.
  • and hence it does not work

a working zip of the example. or any suggestions, would be appreciated.

  • there seems to be no way to fetch a zip of the whole thing?
    well, this is part of larger repo:
    GitHub - Unity-Technologies/iOSNativeCodeSamples
    there is “code” button where you can download zip
    OnGUI function detected on MonoBehaviour, but not called, because IMGUI module is stripped.
    is this still happening after you get whole project? i might need to check if things need to be updated for newer unity versions

I gave up on the small example and moved the code into my main project:

  1. copied the MyAppController.mm into my Plugins/iOS
  2. merged the BackgroundFetch.cs into my main project script

EVERYTHING WORKS! verified using debugger “Simulate Background Fetch”

F.Y.I – I added a way for my main script to set/change the url called:

static NSString* serverUrl;

NSURL* url = [NSURL URLWithString: serverUrl];

extern “C” void NewServerUrl(char* newUrl)
{
serverUrl = @(newUrl);
return;
}

THANKS FOR THIS NATIVE PLUGIN !!

  • I had almost given up on iPhone BackgroundFetch
  • this plugin was exactly what I needed. Thanks Again!
1 Like

Hi - I’m trying to do something similar where I use receive updates to the user’s location in the background and then do something with them (print a debug message containing the coordinates for simplicity). I believe I have all the permissions set correctly in the .plist and have given the app permssion in ios settings to use location while in use (also give access in the background)

  <key>NSLocationWhenInUseUsageDescription</key>
    <string>ARG map</string>
    <key>UIBackgroundModes</key>
    <array>
      <string>location</string>
    </array>

I read in the iOS dev docs that application(_:didFinishLaunchingWithOptions:) gets called when the background location update fires. So I tried doing something similar to the what you did with Data Feching thinking that this would run the update function when the location updates and then in Update I just Debug.Log the location.

#import "UnityAppController.h"
extern bool         _unityAppReady;


@interface MyAppController : UnityAppController
{
}
- (BOOL)application:(UIApplication*)application willFinishLaunchingWithOptions:(NSDictionary*)launchOptions;
@end

@implementation MyAppController
- (BOOL)application:(UIApplication*)application willFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
    // do not try to run player loop before unity is inited
    //if (_unityAppReady)
        UnityBatchPlayerLoop();

    return [super application: application willFinishLaunchingWithOptions: launchOptions];
}

As far as I can tell nothing happens when I go into background mode. Any information / assistance you can provide on how to do this would be greatly appriciated!

-Mike Hein

As far as I can tell nothing happens when I go into background mode.
are you running in xcode? there “Debug > Simulate Location” menu to trigger location updates.
Also, just in case, check
Handling location updates in the background | Apple Developer Documentation
there are more things to do to actually handle it