如何在颤动中收听 UIApplication 生命周期 iOS

Posted

技术标签:

【中文标题】如何在颤动中收听 UIApplication 生命周期 iOS【英文标题】:How can I listen UIApplication lifecycle iOS in flutter 【发布时间】:2019-09-23 15:25:07 【问题描述】:

我想检测我的 ios 应用程序写入时颤动移动到后台并进入前台。我习惯 WidgetsBindingObserver 来听生命周期的颤动。但它并不完全不是我的期望。当我在颤振应用程序生命周期触发器中推送本机控制器时 AppLifecycleState.paused 但我的应用程序正在运行。有人刚刚遇到过这个问题吗?以及您解决问题的方式。谢谢

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) 
    super.didChangeAppLifecycleState(state);
    if(state == AppLifecycleState.paused) 
      _viewModel.applicationMoveToBackground();

     else if(state == AppLifecycleState.resumed) 
      _viewModel.applicationMoveToForeground();
    
  

【问题讨论】:

【参考方案1】:

我通过使用 FlutterBasicMessageChannel 发出 iOS UIApplication 生命周期来颤振解决了这个问题。 在 iOS Runner 应用中:

import UIKit
import Flutter

@UIApplicationMain@objc class AppDelegate: FlutterAppDelegate 

    var applicationLifeCycleChannel: FlutterBasicMessageChannel!

    override func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool 
        GeneratedPluginRegistrant.register(with: self)

        applicationLifeCycleChannel = FlutterBasicMessageChannel(
            name: "applicationLifeCycle",
            binaryMessenger: (window.rootViewController as! FlutterViewController).binaryMessenger,
            codec: FlutterStringCodec.sharedInstance())
        return super.application(application, didFinishLaunchingWithOptions: launchOptions)
    

    override func applicationWillTerminate(_ application: UIApplication) 
        applicationLifeCycleChannel.sendMessage("applicationWillTerminate")
    

    override func applicationWillEnterForeground(_ application: UIApplication) 
        applicationLifeCycleChannel.sendMessage("applicationWillEnterForeground")
    

    override func applicationDidEnterBackground(_ application: UIApplication) 
        applicationLifeCycleChannel.sendMessage("applicationDidEnterBackground")
    


在 Flutter 应用中:

 static const applicationLifecycleChannel = BasicMessageChannel<String>('applicationLifeCycle', StringCodec());
static const kApplicationWillTerminate = 'applicationWillTerminate';
  static const kApplicationWillEnterForeground = 'applicationWillEnterForeground';
  static const kApplicationDidEnterBackground = 'applicationDidEnterBackground';

  @override
  void initState() 
    applicationLifecycleChannel.setMessageHandler((message) async 
      switch(message) 
        case kApplicationWillTerminate:
          break;
        case kApplicationWillEnterForeground:
          _viewModel.applicationMoveToForeground();
          break;
        case kApplicationDidEnterBackground:
          _viewModel.applicationMoveToBackground();
          break;
        default:
          break;
      
      return message;
    );
    super.initState();
  

【讨论】:

【参考方案2】:

你在使用 WidgetsBindingObserver 吗?

class _PageState extends State<Page> with WidgetsBindingObserver 
    @override
    void initState() 
        super.initState();
        WidgetsBinding.instance.addObserver(this);
    

    @override
    dispose() 
        super.dispose();
        WidgetsBinding.instance.removeObserver(this);
    

    @override
    void didChangeAppLifecycleState(AppLifecycleState state) 
        super.didChangeAppLifecycleState(state);
        if(state == AppLifecycleState.paused) 
          _viewModel.applicationMoveToBackground();

         else if(state == AppLifecycleState.resumed) 
          _viewModel.applicationMoveToForeground();
        
    

【讨论】:

是的,我曾经在我的项目中使用上述代码,但是当我从诸如 Image Picker 之类的颤振中推送新的本机控制器时,我不希望应用程序移动到暂停状态。

以上是关于如何在颤动中收听 UIApplication 生命周期 iOS的主要内容,如果未能解决你的问题,请参考以下文章

如何使用插件audio_service颤动处理音频通知的onTap?

在颤动中合并来自 Firestore 的流

如何在 iOS 中侦听通知关闭事件?

在 js 中监听 laravel livewire 生命周期钩子

app 和控制器 的生命周期

UIApplication,UIWindow,UIViewController,UIView(layer)简单介绍