简体   繁体   English

替代 iOS 已弃用的 UIApplicationExitsOnSuspend

[英]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的替代方法,我在后台检查经过的时间,并在需要时重置我的导航:

  • I keep the last time the app did enter in background ( NSUserDefaults in applicationDidEnterBackground )我保留了应用程序最后一次进入后台的时间( NSUserDefaults in applicationDidEnterBackground
  • When the application did become active again I check the saved time (in applicationDidBecomeActive ) against a given time threshold.当应用程序再次变为活动状态时,我根据给定的时间阈值检查保存的时间(在applicationDidBecomeActive中)。
  • I reset my navigation if my threshold is overrun (this allow me to resync my app or re-ask for login after some times, but allowing a quick phone call for exemple)如果超出阈值,我会重置导航(这允许我重新同步我的应用程序或在一段时间后重新请求登录,但允许快速拨打电话)

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.

相关问题 替代已弃用的AudioSessionGetProperty,适用于iOS 7 - Alternative to deprecated AudioSessionGetProperty, for iOS 7 UIApplicationExitsOnSuspend 是否有 iOS 8 的等效项? - UIApplicationExitsOnSuspend is there an equivalent for iOS 8? 当UIApplicationExitsOnSuspend = Yes时,iOS 8崩溃重新启动 - iOS 8 Crash on relaunch when UIApplicationExitsOnSuspend = Yes 在 iOS 16 中,attemptRotationToDeviceOrientation func 已弃用,是否有替代方法? - In iOS 16, attemptRotationToDeviceOrientation func is deprecated,Is there an alternative method? 当没有替代方法时,禁止使用该方法的警告 - Suppress warning for deprecated method when there's no alternative for it iOS 7中kSecTrustResultConfirm的替代方案是什么? - What's the alternative for kSecTrustResultConfirm in iOS 7? Android在iOS上的DownloadManager替代 - Android's DownloadManager alternative on iOS 如果UIAlertView已被弃用,为什么在iOS 8中可以使用? - Why does UIAlertView work in iOS 8 if it's deprecated? 不推荐使用Google plus iOS API来预先填充共享中的数据,还有什么选择 - Google plus iOS API to pre-fill data in share is deprecated, what is the alternative UILabel - 不推荐使用方法的替代方法“adjustsLetterSpacingToFitWidth” - UILabel - Alternative for Deprecated Method “adjustsLetterSpacingToFitWidth”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM