Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

HELP PLS -- Does Unity iOS backgrounding actually work ??

Discussion in 'iOS and tvOS' started by RestlessSwords, May 14, 2022.

  1. RestlessSwords

    RestlessSwords

    Joined:
    May 9, 2021
    Posts:
    26
    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.
     
  2. Petras-Unity

    Petras-Unity

    Unity Technologies

    Joined:
    Oct 7, 2020
    Posts:
    24
  3. RestlessSwords

    RestlessSwords

    Joined:
    May 9, 2021
    Posts:
    26
    @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.
     
    Last edited: Jun 28, 2022
  4. Alexey

    Alexey

    Unity Technologies

    Joined:
    May 10, 2010
    Posts:
    1,602
    > - there seems to be no way to fetch a zip of the whole thing?
    well, this is part of larger repo:
    https://github.com/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
     
  5. RestlessSwords

    RestlessSwords

    Joined:
    May 9, 2021
    Posts:
    26
    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!
     
  6. indoflaven

    indoflaven

    Joined:
    Sep 27, 2017
    Posts:
    5
    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)


    Code (CSharp):
    1.   <key>NSLocationWhenInUseUsageDescription</key>
    2.     <string>ARG map</string>
    3.     <key>UIBackgroundModes</key>
    4.     <array>
    5.       <string>location</string>
    6.     </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.

    Code (CSharp):
    1.  
    2. #import "UnityAppController.h"
    3. extern bool         _unityAppReady;
    4.  
    5.  
    6. @interface MyAppController : UnityAppController
    7. {
    8. }
    9. - (BOOL)application:(UIApplication*)application willFinishLaunchingWithOptions:(NSDictionary*)launchOptions;
    10. @end
    11.  
    12. @implementation MyAppController
    13. - (BOOL)application:(UIApplication*)application willFinishLaunchingWithOptions:(NSDictionary*)launchOptions
    14. {
    15.     // do not try to run player loop before unity is inited
    16.     //if (_unityAppReady)
    17.         UnityBatchPlayerLoop();
    18.  
    19.     return [super application: application willFinishLaunchingWithOptions: launchOptions];
    20. }
    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
     
  7. Alexey

    Alexey

    Unity Technologies

    Joined:
    May 10, 2010
    Posts:
    1,602