Programmatically closing an app.

Here's my context:
  1. User imports a backup file via airdrop, from a laptop to the mobile.

  2. My app opens and asks for a password to import and decrypt the file.

  3. The user decides to cancel instead, so taps the cancel button.

How do I close the app at that point? I have done a lot of googling but so far have not found out how to do it.
Does it tap Cancel in the App ?

Try to call exit(0) in the IBAction of the cancel button.

Don't forget to close the thread if that's OK.
Sorry, I didn't see your reply. Yes, exit(0) closes the app okay, but it means that things don't work properly the next time the user tries to import the backup file. The app will open, but OpenURLContexts doesn't run.
Actually, it just occurred to me that is the actual issue. If the app isn't sitting in the background, and is starting from scratch, OpenURLContexts does not run, so importing a backup file will not work even if the user hasn't canceled. I need a way to make it run.
I tested this in simulator and it worked:
Code Block
          DispatchQueue.main.asyncAfter(deadline: .now()) {
                  UIApplication.shared.perform(#selector(NSXPCConnection.suspend))
              }

But not sure that would pass the appstore review
https://stackoverflow.com/questions/13989030/is-there-any-way-to-programmatically-send-my-iphone-app-to-the-background
Thanks, Claude31

I was wondering the same thing about exit(0). Quite a few people say that would be rejected, but then I see others who have had no issue with it. I have an app on my phone where I think the developer has exit(0) in the cancel button. It certainly looks like it when I press the cancel button.
There is a way around my issue, and that is to put instructions in my help guide to make sure that the app is running before importing a backup. Problem solved, but it feels to me like a bit of a copout.

I was wondering the same thing about exit(0). Quite a few people say that would be rejected,

Old App Store Review Guidelines had an explicit description about quitting apps which can be interpreted as apps should not quit itself,
and as you found, there were some reports that apps had been rejected because of exit.

I cannot find the same description in the latest App Store Review Guidelines nor the latest Human Interface Guidelines.

You should better read them carefully (maybe you have read them several times, then once again), and you can challenge if you think it to be safe enough to use exit, with your own risk.
I can't say that I have properly read them yet, but I'll make certain that I do.

I tried ApplicationSuspendOn in info.plist, but AppStore refused the download. In a earlier version of my app it was allowed to do this.

Interesting comment. Thanks.

Programmatically closing an app.
 
 
Q