0
0

More than 1 year has passed since last update.

【Flutter】プラグインでiOSのrootViewControllerを取得する方法

Posted at

概要

FlutterプラグインでiOSネイティブ画面を利用するために、rootViewControllerを取得する方法を整理します。
こちらの記事を参考にさせていただきました(ただただ感謝✨✨)。プラグイン作成時にはself経由でrootViewControllerを取得できず、代替手段が必要になるため、その補足記事となります。

環境

$ flutter --version
Flutter 2.5.3 • channel stable • https://github.com/flutter/flutter.git
Framework • revision 18116933e7 (3 weeks ago) • 2021-10-15 10:46:35 -0700
Engine • revision d3ea636dc5
Tools • Dart 2.14.4

実装

UIApplication経由でrootViewControllerを取得します。
これにより、プラグイン側で独自に画面を扱うことができます。
※念のため、nil時でも問題にならないようにguard let構文を採用しています。

guard let rootViewController = UIApplication.shared.windows.filter({ (w) -> Bool in
    return w.isHidden == false
}).first?.rootViewController else {
    result(failMap(errorMessage: "rootViewController is nil."))
    return
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0