SDK version and applicationWillTerminate issue - save game

Hi - I have a problem and don’t see a way around it yet.

My game is essentially done - it’s even been approved. But I just noticed a big problem concerning iOS4.

I am using PlayerPrefs to save game data and this works fine except in the following situation. If I build my Unity project using SDK “iPhone 3.0” this allows me to deploy on devices running a pre-3.2 OS. To build this, I must set the base SDK to 4.0 in XCode (since my XCode did not ship with 3.0) and set the deployment target to 3.0. Since iOS4 calls applicationWillResignActive() instead of applicationWillTerminate(), that means my PlayerPrefs do not get saved.

So to summarise:

In Unity:

SDK iPhone 3.0

In XCode:

Base SDK: 4.0
Deployment Target: 3.0

On iPhone 3GS running iOS 4.0:

No applicationWillTerminate() hence no OnApplicationQuit() in Unity... hence no PlayerPrefs save.

On iPod Touch 1st gen running iOS 3.1.3:

Runs fine because it calls applicationWillTerminate() as intended

How are people managing this? Is it possible to put the UnityCleanup() call into the applicationWillResignActive() implementation?

This is an urgent issue for me if anyone can help please!

terminate was never intend to be used for that.

the active handling callbacks are meant to be used for that (resign active etc) where unity normally is paused / unpaused

thats the point where you are meant to store your stuff.

if you want to do it from within unity: OnApplicationPause( bool pause ) is your friend :slight_smile:

Ok I think I have something for this…

As dreamora correctly states in this post (http://forum.unity3d.com/viewtopic.php?t=56042) you can add a key to your plist file to force the app to “quit on suspend”. This ensures applicationWillTerminate() gets called and therefore the OnApplicationQuit message is sent ( = PlayerPrefs are happy again).

To clarify, if you do not want your app to run in the background, add this to your plist file (right click on the plist file, then OpenAs->Source Code file)

	<key>UIApplicationExitsOnSuspend</key>
	<true/>

(thanks for the tip dreamora!) :wink:

You posted at the same time :slight_smile:

Well my situation has been resolved one way or the other now (and it was thanks to you), but I realise my multitasking has been completely avoided…

edit: I’d be a lot happier about using OnApplicationPause (another really good tip) but I really need the lowest risk, simplest solution right now given that I cannot afford to introduce bugs. I can’t wait to share the game with you guys… should be out next week but I can’t talk about it yet!

Thats naturally a way too, if you don’t want it to remain there :slight_smile:

(there might be cases where you want that, for example if you know that your application eats a host of data or if you didn’t design your app for multitasking and have Time.time based measuring in that will break with multitasking etc)