How to change the app status when the app is turned off in Swift5?

I have no problem running my app.



However, if you click the Share button on my app and complete the share, turn off my app and click on the shared image on another app, it will not go to the login screen. Why doesn't it recognize the background status?




switch UIApplication.shared.applicationState {
case .background:
    LoginScreen()
    break
case .inactive:
    WebViewScreen()
    break
case .active:
    WebViewScreen()
    break
default:
    break
}
return true




So if I press the Shared button and go to my app, I can go to the webview screen without fail. But I don't want to go to the webview screen when the app is off.



> Table 2-3 App states State

>

> Description

>

> Not running

>

> The app has not been launched or was running but was terminated by the

> system.

>

> Inactive

>

> The app is running in the foreground but is currently not receiving

> events. (It may be executing other code though.) An app usually stays

> in this state only briefly as it transitions to a different state.

>

> Active

>

> The app is running in the foreground and is receiving events. This is

> the normal mode for foreground apps.

>

> Background

>

> The app is in the background and executing code. Most apps enter this

> state briefly on their way to being suspended. However, an app that

> requests extra execution time may remain in this state for a period of

> time. In addition, an app being launched directly into the background

> enters this state instead of the inactive state. For information about

> how to execute code while in the background, see Background Execution.

>

> Suspended

>

> The app is in the background but is not executing code. The system

> moves apps to this state automatically and does not notify them before

> doing so. While suspended, an app remains in memory but does not

> execute any code.

>

> When a low-memory condition occurs, the system may purge suspended

> apps without notice to make more space for the foreground app.



The 2016 Apple document had a status value of 'Notruning'. Is it gone now? Is there a substitute status value?

Replies

There is no API that returns ‘not running’ because, if your app is not running, there’s no way it could call that API in order to get back that value. This not-running state makes sense from an outside perspective, but not from not from the inside.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

@eskimo So is there any way you can solve this problem?

So is there any way you can solve this problem?

I’m not actually sure what your problem is. In your first post, what do yo mean by “turn off my app”?

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

I mean, I turned off the app. I completely turned off the app behind me. And in time, I click on the data I shared on another app to move to my app, but I don't move to the login screen, I just watch the webview screen.

Try something like this (Swift 4?) and see if the print statement says the same state you expect:


let appstate = UIApplication.shared.applicationState
  switch appstate {
  case .active:
  print("the app is in active state")
  case .background:
  print("the app is in background state")
  case .inactive:
  print("the app is in inactive state")
  default:
  print("the default state")
  break
  }



Perhaps you want the default to be the login screen?

I completely turned off the app behind me.

I’m sorry but “turn off” is not part of the standard iOS lexicon. Perhaps you could post some screen shots of what you’re doing.

IMPORTANT DevForums does not let you post screen shots, so you’ll need to upload the screen shots to a image hosting server and post the URLs. That’ll require moderation, but that shouldn’t be a problem.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Address with video.

Cool. Alas, I still don’t understand what I’m looking at. Can you walk me through the process of what you’re trying to illustrate, with time stamps that line up to that video.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

0: 00 ~ 0:16 : Share my app posts to other apps 0: 16 ~ 0 : 18 : Shut down all the apps that are running on your phone. 0: 18 ~ 0: 30 : Access the app that has the shared posts and click the posts. Then my app will run 0:30 ~ end : However, the first screen I see in my app should be the login screen, but it is the webview screen.

I'm still stuck in this problem. Is there any solution?