Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I "push" a UIViewController from FlutterViewController

I want to "push" a new native view (Activity as in Android & UIViewController as in iOS) upon our Flutter view, and after that new native view is completed/finished, make screen come back to our Flutter view.

I can do this in Android. But I'm very new to iOS, when I try to do this in ios/Runner/AppDelegate.m:

  SwiftViewController *swiftViewController = [controller.storyboard instantiateViewControllerWithIdentifier:@"SwiftViewController"];
  [(FlutterViewController *)self.window.rootViewController pushViewController:swiftViewController animated:YES];

It gives error:

No visible @interface for "FlutterViewController" declared the selector 'pushViewController:animated'

So how to do it in iOS? Thanks

like image 397
Eason PI Avatar asked Aug 12 '17 02:08

Eason PI


People also ask

How do I push a view controller?

Pushing a view controller causes its view to be embedded in the navigation interface. If the animated parameter is true , the view is animated into position; otherwise, the view is simply displayed in its final location.

What is the role of UIViewController?

The UIViewController class defines the shared behavior that's common to all view controllers. You rarely create instances of the UIViewController class directly. Instead, you subclass UIViewController and add the methods and properties needed to manage the view controller's view hierarchy.

What is AppDelegate flutter?

The FlutterAppDelegate performs functions such as: Forwarding application callbacks such as openURL to plugins such as local_auth. Forwarding status bar taps (which can only be detected in the AppDelegate) to Flutter for scroll-to-top behavior.


1 Answers

In Swift, We can open native iOS viewController from a flutter action event using the method channel, In flutter, you need to add the following lines to trigger the native iOS method. Add these lines in your action method

var platform = const MethodChannel('navigation');
final String result = await platform.invokeMethod('IOS');

In iOS, add the following lines in the AppDelegate application method.

let controller : FlutterViewController = window?.rootViewController as! FlutterViewController
let methodChannel = FlutterMethodChannel(name: "navigation", binaryMessenger:controller.binaryMessenger)
navigationController = UINavigationController(rootViewController: controller)
navigationController.setNavigationBarHidden(true, animated: false)
self.window!.rootViewController = navigationController
self.window!.makeKeyAndVisible()
methodChannel.setMethodCallHandler({
    (call: FlutterMethodCall, result: @escaping FlutterResult) -> 
    Void in
if call.method == "IOS" {
      let vc = "Your viewController"(nibName: "Your 
      viewController", bundle: nil)
      navigationController.pushViewController(vc, animated: 
     false)
} else {
         result(FlutterMethodNotImplemented)
         return
}
})

Native view controller back button action.

self.navigationController?.popViewController(animated: true)

Thank you.

like image 121
Rajesh Avatar answered Oct 17 '22 04:10

Rajesh