[英]Alternative to iOS's deprecated UIApplicationExitsOnSuspend
I have an app that I do NOT want to run in the background.我有一个不想在后台运行的应用程序。 There is nothing my app can do in background mode, and allowing it to only adds annoying UI complications.
我的应用在后台模式下无能为力,只会增加烦人的 UI 复杂性。 I want the app to terminate when it is removed from the foreground.
我希望应用程序在从前台删除时终止。
UIApplicationExitsOnSuspend
was Apple's method of allowing this before, and it worked perfectly for my use case. UIApplicationExitsOnSuspend
是 Apple 之前允许这样做的方法,它非常适合我的用例。 However, it is now deprecated and they are rejecting apps with that Info.plist value.但是,它现在已被弃用,他们正在拒绝具有该 Info.plist 值的应用程序。
The only alternative I've been able to find is exit(0)
in an app delegate method, but Apple strongly discourages this, and it appears as a crash.我能找到的唯一替代方法是应用程序委托方法中的
exit(0)
,但 Apple 强烈反对这样做,并且它看起来像一个崩溃。
Is there any other viable alternative?还有其他可行的选择吗? I simply do not want my app to run in the background, all it does is drain a user's battery unnecessarily.
我只是不希望我的应用程序在后台运行,它所做的只是不必要地耗尽用户的电池。
As an alternative to UIApplicationExitsOnSuspend
I check the elapsed time in background an reset my navigation if needed:作为
UIApplicationExitsOnSuspend
的替代方法,我在后台检查经过的时间,并在需要时重置我的导航:
NSUserDefaults
in applicationDidEnterBackground
)NSUserDefaults
in applicationDidEnterBackground
)applicationDidBecomeActive
) against a given time threshold.applicationDidBecomeActive
中)。 Since it's an old app here is the objective-c:由于它是一个旧应用程序,这里是 objective-c:
appdelegate.m: appdelegate.m:
- (void)applicationDidEnterBackground:(UIApplication *)application {
NSLog(@"APPLIFE : going to sleep ... ");
[[NSUserDefaults standardUserDefaults] setObject: [NSDate date] forKey:@"last-background-time"];
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
NSLog(@"APPLIFE : ... back to front");
NSDate * bgTime = [[NSUserDefaults standardUserDefaults] objectForKey:@"last-background-time"];
NSTimeInterval secondsSinceLastRun = fabs([bgTime timeIntervalSinceNow]);
double threshold = 60 * 60 * 24; //in seconds.
if (secondsSinceLastRun > threshold) {
NSLog(@"APPLIFE : Too old, reboot app");
//[navigationController popToRoot] or whatever you want
}
}
For SwiftUI, Add this to the App struct:对于 SwiftUI,将其添加到 App 结构中:
@UIApplicationDelegateAdaptor(AppDelegate.self) var delegate
then add these two classes:然后添加这两个类:
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(
_ application: UIApplication,
configurationForConnecting connectingSceneSession: UISceneSession,
options: UIScene.ConnectionOptions
) -> UISceneConfiguration {
let sceneConfig = UISceneConfiguration(name: nil, sessionRole: connectingSceneSession.role)
sceneConfig.delegateClass = SceneDelegate.self // 👈🏻
return sceneConfig
}
} }
class SceneDelegate: NSObject, UIWindowSceneDelegate { class SceneDelegate:NSObject,UIWindowSceneDelegate {
func sceneWillResignActive(_ scene: UIScene) { // This will force the app to start fresh each time after two seconds (so the close animation looks clean) do { sleep(2) } exit(0) } } func sceneWillResignActive(_ scene: UIScene) { // 这将强制应用程序在两秒后每次重新启动(因此关闭的 animation 看起来很干净) do { sleep(2) } exit(0) } }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.