我需要检查应用程序是否移动到了后台。为什么?Swift 3:检查应用程序是否在后台
那么因为我的应用程序使用蓝牙,一次只能有一个人连接到此设备。因此,如果他们没有使用它并且应用程序位于后台,请断开它们并将它们发送到连接主页面。
现在我已经完成了这个。我在主要的第一类中有一个选择器,并且具有断开连接并发送到第一页的功能。但是我没有意识到的是,如果控制面板被拖拽,应用程序将处于“背景”。
从环视看来,似乎没有办法检测控制面板是否长大。那么,有没有人有任何想法可以做到这一点?
实际上,我只是想要它,所以如果应用程序被移动到背景以外的任何其他原因比控制面板被提出,断开设备。
选择:
let notificationCenter = NotificationCenter.default
notificationCenter.addObserver(self, selector: #selector(appMovedToBackground), name: Notification.Name.UIApplicationWillResignActive, object: nil)
功能:
@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")
}
}
这不是其他问题重复。我知道如何检查应用程序是否处于后台状态。我只是不想在控制面板被拔出时断开连接...
UIApplicationDidEnterBackground通知是您要查找的内容吗? –