Swift 3: checking for an application in the background

I need to check if the application is moved to the background. Why?

Good, because my application works with bluetooth, and only one person can be connected to this device at a time. Therefore, if they do not use it and the application is in the background, disable them and send them to the main connection page.

Now I have achieved this. I have a selector in the main first class and a function to disable and send to the first page. But I did not understand that if the control panel is being dragged, the application is in the "background".

From looking around, there seems to be no way to determine if the control panel is rising. So does anyone have any ideas on how I can do this differently?

Actually, I just want the application to be moved to the background for some other reason, except for the raised control panel, to disconnect from the device.

Selection:

let notificationCenter = NotificationCenter.default
notificationCenter.addObserver(self, selector: #selector(appMovedToBackground), name: Notification.Name.UIApplicationWillResignActive, object: nil)

Functions:

@objc func appMovedToBackground() {
        if (ViewController.connectedPeripheral != nil) {
            print("App moved to background!")
            let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
            let nextViewController = storyBoard.instantiateViewController(withIdentifier: "connectView") as! ViewController
            self.navigationController?.pushViewController(nextViewController, animated: true)
            ViewController.centralManager.cancelPeripheralConnection(ViewController.connectedPeripheral!)
        }
        else {
            print("App moved to background but no device is connected so no further action taken")
        }
    }

This is not a duplicate of other issues. I know how to check if an application is in the background. I just don't want to turn off when the control panel rises ...

+4
source share
3 answers

In Swift:

if UIApplication.shared.applicationState == .background {
    // Add code here...
}

In Objective-C:

if ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground) {
    // Add code here...
}

Hope it works!

+2
source

Have you tried adding an observer in willResignActive to your controller?

NotificationCenter.default.addObserver(self, selector: #selector(willResignActive), name: .UIApplicationWillResignActive, object: nil)


func willResignActive(_ notification: Notification) {
    // code to execute
}
+1
source

. .

override func viewDidLoad() {
    super.viewDidLoad()

    let app = UIApplication.shared

    //Register for the applicationWillResignActive anywhere in your app.
    NotificationCenter.default.addObserver(self, selector: #selector(ViewController.applicationWillResignActive(notification:)), name: NSNotification.Name.UIApplicationWillResignActive, object: app)
}

func applicationWillResignActive(notification: NSNotification) {

}
0

Source: https://habr.com/ru/post/1682188/


All Articles