swift3 how to Change screen when app changes to background status

2k views Asked by At

in swift3

I want to change the screen to hide the original screen when the app is in the background state.

For example, if you press the home button twice while the app is running, the screen will change to a different screen.

I want to set the screen to change to LaunchScreen.

Thank you for your help.

4

There are 4 answers

1
AudioBubble On

"If you do not want your application to remain in the background when it is quit, you can explicitly opt out of the background execution model by adding the UIApplicationExitsOnSuspend key to your application’s Info.plist file and setting its value to YES.

When an application opts out, it cycles between the not running, inactive, and active states and never enters the background or suspended states.

When the user taps the Home button to quit the application, the applicationWillTerminate: method of the application delegate is called and the application has approximately five seconds to clean up and exit before it is terminated and moved back to the not running state." s

1
Tejas Ardeshna On

Try this one:

func applicationDidEnterBackground(_ application: UIApplication) {

    let imageView = UIImageView(frame: self.window!.bounds)
    imageView.tag = 1001
    imageView.image = UIImage(named: "") //your image goes here

    UIApplication.shared.keyWindow?.subviews.last?.addSubview(imageView)
 }

func applicationWillEnterForeground(_ application: UIApplication) {
    if let imageView : UIImageView = UIApplication.shared.keyWindow?.subviews.last?.viewWithTag(1001) as? UIImageView {
        imageView.removeFromSuperview()
    }

}
0
Hodson On

Add this function in your AppDelegate.

func applicationWillResignActive(_ application: UIApplication) {
    // Change the view to show what you want here.
}

This method is called to let your app know that it is about to move from the active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the app and it begins the transition to the background state.

Source: https://developer.apple.com/reference/uikit/uiapplicationdelegate/1622950-applicationwillresignactive

0
D4ttatraya On

This is common scenario where we want to avoid screen-shotting by iOS while going to BG or covering app screens when app is in stack.

Here is what I'm doing:

class AppDelegate: UIResponder, UIApplicationDelegate {

  var window: UIWindow?
  private var appCoverWindow: UIWindow?
  private var appCoverVC: UIViewController?

  func applicationDidBecomeActive(_ application: UIApplication) {
    if appCoverWindow != nil {
        appCoverWindow!.isHidden = true
        appCoverWindow!.rootViewController = nil
        appCoverWindow = nil
        appCoverVC = nil
    }
  }

  func applicationWillResignActive(_ application: UIApplication) {
    appCoverVC = rootViewController().storyboard!.instantiateViewController(withIdentifier: "AppCoverVCId") as! AppCoverViewController
    appCoverWindow = UIWindow(frame: UIScreen.main.bounds)
    let existingTopWindow = UIApplication.shared.windows.last
    appCoverWindow!.windowLevel = existingTopWindow!.windowLevel + 1
    appCoverVC!.view.frame = appCoverWindow!.bounds
    appCoverWindow!.rootViewController = appCoverVC!
    appCoverWindow!.makeKeyAndVisible()
  }

  class func appLaunchImage() -> UIImage? {
    //this method will return LaunchImage
    let launchImageNames = Bundle.main.paths(forResourcesOfType: "png", inDirectory: nil).filter { (imageName) -> Bool in
        return imageName.contains("LaunchImage")
    }

    for imageName in launchImageNames {
        guard let image = UIImage(named: imageName) else { continue }

        // if the image has the same scale and dimensions as the current device's screen...
        if (image.scale == UIScreen.main.scale) && (image.size.equalTo(UIScreen.main.bounds.size)) {
            return image
        }
    }
    return nil
  }
}

Instead of using UIWindow to cover app, we can directly use UIViewController also, but that may cause issues if keyboard is present while going to BG.

Here is AppCoverViewController.swift:
(It has XIB in storyboard with one full screen UIImageView)

class AppCoverViewController: BaseViewController {

  @IBOutlet weak var bgImageView: UIImageView!//full screen image view

  override func viewDidLoad() {
    super.viewDidLoad()
    if let image = AppDelegate.appLaunchImage() {
        bgImageView.image = image
    }
  }

  override func deviceOrientationDidChange() {
    if let image = AppDelegate.appLaunchImage() {
        bgImageView.image = image
    }
  }
}

This class takes care of device rotations also.