How can I close app in iOS?

Hello, there.

Here is my logic of the end of my app.

  1. When the user finish the game app, the app have to send an email with the information of score and screen shot via email.

  2. Then the app have to be closed automatically.

I used os.exit() for exit app after send email.

But the app closes without any email sending popup.

So I thought I have to don’t close app, just switch to background of device.

But how…?

Any body who know this, please help me.

Thank you.

Jacky

You can’t terminate iOS apps from within themselves. And you shouldn’t use that call at all… not on Android either. See here:

https://docs.coronalabs.com/api/library/os/exit.html

If your app opens up the Mail app, then your game will automatically be backgrounded.

Best regards,

Brent

Thank you for your reply.

I tried with os.exit().

This is the source code of that part.


if (native.canShowPopup(“mail”)) then

    local options =

    {

        to = user_email,

        subject = “Your child’s Score in Matthew Can Lite”,

        body = calc_result,

        attachment =

        {

            baseDir=system.DocumentsDirectory,

            filename=“activity.png”,

            type=“image/png”

        }

    }

    native.showPopup( “mail”, options )

else

    native.showAlert( “Alert!”, “Emailing from this app not available on this device”, { “OK” } )

end

os.exit()


But it close the app before sending email.

I don’t know why the app is closes first, before show popup.

Could you explain this?

Thank you.

Jacky

Hi Jacky,

Please don’t use “os.exit()”. Apple and Google will reject your app (once submitted to them) if you use this, because it will appear as if the app “crashed”.

On Android, use “native.requestExit()”, and on iOS, you can’t force-close the app from within itself.

Brent

Oh, I see.

Thank you very much for your advice.

So how can I do this job?

Can I go to background screen with out os.exit()? - just like exit app for users.

I want to solve this problem quickly.

So please help me.

Thank you, Brent.

Jacky

Hi Jacky,

On Android, you can call “native.requestExit()”, but what is your specific reason for wanting to exit the app after sending the email?

Brent

Well, actually I wanted to send email with score and screen shot when the users complete the game and just before close the game, you know.

There are many games that shows the users’ score at the end of game.

I just want to do that.

So I think if I don’t use os.exit()- in other words, if I can’t exit the app, is there any way to show the users that the app is closed?

That’s what I want exactly.

Thank you.

Jacky

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?

You can’t terminate iOS apps from within themselves. And you shouldn’t use that call at all… not on Android either. See here:

https://docs.coronalabs.com/api/library/os/exit.html

If your app opens up the Mail app, then your game will automatically be backgrounded.

Best regards,

Brent

Thank you for your reply.

I tried with os.exit().

This is the source code of that part.


if (native.canShowPopup(“mail”)) then

    local options =

    {

        to = user_email,

        subject = “Your child’s Score in Matthew Can Lite”,

        body = calc_result,

        attachment =

        {

            baseDir=system.DocumentsDirectory,

            filename=“activity.png”,

            type=“image/png”

        }

    }

    native.showPopup( “mail”, options )

else

    native.showAlert( “Alert!”, “Emailing from this app not available on this device”, { “OK” } )

end

os.exit()


But it close the app before sending email.

I don’t know why the app is closes first, before show popup.

Could you explain this?

Thank you.

Jacky

Hi Jacky,

Please don’t use “os.exit()”. Apple and Google will reject your app (once submitted to them) if you use this, because it will appear as if the app “crashed”.

On Android, use “native.requestExit()”, and on iOS, you can’t force-close the app from within itself.

Brent

Oh, I see.

Thank you very much for your advice.

So how can I do this job?

Can I go to background screen with out os.exit()? - just like exit app for users.

I want to solve this problem quickly.

So please help me.

Thank you, Brent.

Jacky

Hi Jacky,

On Android, you can call “native.requestExit()”, but what is your specific reason for wanting to exit the app after sending the email?

Brent

Well, actually I wanted to send email with score and screen shot when the users complete the game and just before close the game, you know.

There are many games that shows the users’ score at the end of game.

I just want to do that.

So I think if I don’t use os.exit()- in other words, if I can’t exit the app, is there any way to show the users that the app is closed?

That’s what I want exactly.

Thank you.

Jacky