How can I close app in iOS?

HI Jacky,

You want the user to email a score to who? Or you want to send them an email?

Brent

@Jacky, why send an email on close? Is it so you have their score? If so I suggest you have a web host and use a network.request() to upload that info.

If that is out of your comfort zone then look at something like game sparks or AWS (needs some coding on backend) to store your data.

If this info is FYI then use a network.request() in applicaton.suspend (Yes Brent will protest a bit) to send simple data to your web host and then use that data on your web host to ping the info to you.  In my testing you have about 1 second on suspend to “do work” before the thread is terminated on Android and iOS

I would *not* recommend doing any form of networking in application.suspend.

Networking has latency issues and the time allotted “to do work” is not nearly long enough to guarantee successful operation.

Hi Jacky,

Don’t do anything network-related as the applications suspends (or exits)… it’s highly risky and you aren’t guaranteed to have 1 second (or more, or less) before the app is terminated, thus killing the process.

Brent

The point I was making is the time allocated is enough to send a simple network request.  Most of the time ensuring success by processing a callback handler is not required and a simple isset() check in PHP is enough to ascertain if you have a complete POST.

Please refer to the Apple documentation (especially the section titled “What to Do When Your App Is Interrupted Temporarily” - https://developer.apple.com/library/content/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/StrategiesforHandlingAppStateTransitions/StrategiesforHandlingAppStateTransitions.html.  Where Apple recommends to “Save data and any relevant state information” following an applicationWillResignActive() method.

Google recommends using onStop() method to save state - https://developer.android.com/guide/components/activities/activity-lifecycle.html#onstop/  Particularly they recommend “if you can’t find a more opportune time to save information to a database, you might do so during onStop()

So Corona recommendation is somewhat at odds with app Store recommendations.  Although I appreciate the “don’t do it stance” means there can be no failure but sometimes that is the only chance to save state without constantly saving state.

I have to disagree @sgs. onSuspend is a great time to save data locally which is probably going to happen in a few milliseconds in most cases. Saving data to an online data store may not be safe.

Network requests can take a significantly longer time. I’m on a fiber connection with single digit ping times for my home computer and my device’s on wifi. Assuming the server is fast enough, it shouldn’t be a problem. Drop down to one bar of LTE or drop off of LTE on to 3G and even a fast server connection may have too much latency to be safe when suspending. You need to assume your players are going to have terrible network connections.

I personally save data right after I change it. That way I don’t have to worry about saving it during a suspend. 

Rob

Totally agree, state should be saved during run time too and not only on suspend.  And I also agree with the stance of don’t do heavy work in suspend.

My point is you can do light work on suspend if you accept the fact that it might not complete under some scenarios (as you stated).  If the design isn’t based on it “always working” and you code a solution that is more “if it works then it is a bonus” then I personally do not see the harm in trying.  This is definitely a risk v reward scenario.  In my case it is only for backup and my server is hardened against incomplete POSTs.

In an ideal world, Corona would allow background threading as that would allow for this and open other possibilities of app types too.  Is there a reason Corona can’t run in the background?  If openGL, openAL, etc was suspended?