要求区分这两种状态:
首先,当应用程序打开并且用户按下锁定按钮时,手机被锁定并且应用程序进入后台。
其次,当应用程序打开并且用户按下主页按钮或向上滑动时,应用程序进入后台,但手机保持解锁状态。
在这两种情况下,应用程序都会进入后台,因此无法检测它是否使用锁定按钮或简单的主页按钮向上滑动进入后台。
我在 AppDelegate.swift 文件中尝试了这 3 个应用程序生命周期方法的组合,以检测应用程序是否通过按锁定按钮或向上滑动主页按钮进入后台:
在 AppDelegate.swift 内的应用程序函数中将这 3 个标志变量初始化为 false:
let willResignActive = UserDefaults.standard
let didBecomeActive = UserDefaults.standard
let didEnterBackground = UserDefaults.standard
willResignActive.set(false, forKey: "willResignActive")
didBecomeActive.set(false, forKey: "didBecomeActive")
didEnterBackground.set(false, forKey: "didEnterBackground")
这是我的 AppDelegate.swift 文件代码:
import UIKit
import Capacitor
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let willResignActive = UserDefaults.standard
let didBecomeActive = UserDefaults.standard
let didEnterBackground = UserDefaults.standard
willResignActive.set(false, forKey: "willResignActive")
didBecomeActive.set(false, forKey: "didBecomeActive")
didEnterBackground.set(false, forKey: "didEnterBackground")
return true
}
func applicationWillResignActive(_ application: UIApplication) {
print("App will resign active!")
let willResignActive = UserDefaults.standard
willResignActive.set(true, forKey: "willResignActive")
}
func applicationDidEnterBackground(_ application: UIApplication) {
print("App did enter background!")
let didEnterBackground = UserDefaults.standard
didEnterBackground.set(true, forKey: "didEnterBackground")
}
func applicationDidBecomeActive(_ application: UIApplication) {
print("App did become active!")
let didBecomeActive = UserDefaults.standard
didBecomeActive.set(true, forKey: "didBecomeActive")
}
}
假设如果这三个生命周期方法中的任何一个都没有在两个场景(按下锁定按钮或按下主页按钮)中被调用,那么我可以检测到差异。但由于上述所有函数都在这两种情况下执行,所以所有 3 个标志都设置为 true,因此我无法区分应用程序是使用锁定按钮还是主页按钮进入后台!
好吧,无论所有过程如何,您都需要经验丰富的网络专家的服务,这将能够帮助您摆脱困境但值得庆幸的是,在专业人士 bwh240pty@gmail 的帮助下可以恢复并恢复