Change iOS rootViewController with Flutter

This article based on Flutter v0.7.3

iOS rootViewController is FlutterViewController by default.

I wanted to change this, so I did the following:

1. Remove Main.storyboard

FlutterViewController has been set to rootViewController by Main.storyboard, I deleted it.

  • Remove UIMainStoryboardFile in ios/Runner/Info.plist
  • Remove ios/Runner/Base.lproj/Main.storyboard

2. Set rootViewController

I edited AppDelegate.swift and set rootViewController in code.

@objc class AppDelegate: FlutterAppDelegate {
var flutterViewController: FlutterViewController?

...

and

override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
self.flutterViewController = FlutterViewController()
self.window = UIWindow.init(frame: UIScreen.main.bounds)
self.window.rootViewController = MyNavigationController.init(rootViewController: self.flutterViewController!)
self.window.makeKeyAndVisible()

3. Override some methods

Some methods of FlutterAppDelegate assume that the rootViewController is FlutterViewController. That is, we need to override them.

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesBegan(touches, with: event)

if let vc = UIApplication.shared.keyWindow?.rootViewController as? FlutterViewController {
return
}

self.flutterViewController?.handleStatusBarTouches(event)
}

override func registrar(forPlugin: String) -> FlutterPluginRegistrar {
return (self.flutterViewController?.registrar(forPlugin: forPlugin))!
}

override func hasPlugin(_ pluginKey: String) -> Bool {
return (self.flutterViewController?.hasPlugin(pluginKey))!
}

override func valuePublished(byPlugin pluginKey: String) -> NSObject {
return (self.flutterViewController?.valuePublished(byPlugin: pluginKey))!
}
Like what you read? Give Tetsuhiro Ueda a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.