Open native UIViewController in Flutter

3,005

Solution 1

It's completely fine to use Native API's while creating a cross platform application but make sure there is no other way to achieve your goal. Many a times(when a cross platform engine/sdk is new) you have no choice other than using the native code to complete your functionality.

Not sure about your goal but the common issue with cross platform engine/sdk's(e.g, Flutter) is that the third party SDK/API's(e.g, Chartboost, Stripe, Twilio) takes some time to translate into that engine/sdk you are using so while its not available you have no choice except to call those API's natively.

Other issue happens is some platform specific API's e.g, Camera, Contacts, In-App, GameCenter etc do not come along with the cross platform SDK or it takes time to get some wrapper for such API's so in all such cases you can use Native API's.

Solution 2

As Kamran suggests, its okay to use Native code in your flutter application. Specifically where any particular features are not feasible or SDK/API's are not available for flutter yet. Flutter documentation has detailed info on using native code in the flutter application. Reading through the documentation will help understand the architecture. Here's the link for official documentation

This is not what you may want for this particular problem, but alternatively, you can use PlatformViews introduced by flutter some time back. PlatformViews allow you add native views to your widget hierarchy. You'll be able to add custom view modules to your flutter UI. Here is a great blog to help understand how to implement PlatformViews.

Hope this add a little more clarity on how Flutter can be used to its full potential.

PS: I am new to posting answers on stack overflow. If there are any mistakes, please do let me know so that i can rectify them.

Share:
3,005
Tom
Author by

Tom

Updated on December 11, 2022

Comments

  • Tom
    Tom over 1 year

    I have an application I develop in Flutter, and it has one UIViewController that has to be implemented in native IOS (there is no other workaround, it is a must)

    I have implemented it, it works according to the first tests, but I'd like to double check if it is okkey by your opinion, as I am not so experienced in Flutter, and I am afraid a bit a make some mess in my app navigation stack that can cause bugs in the future.

    So, I have implemented it by MethodChannel. I have a method, that called from 'Flutter side'. I don't paste my MethodChannel related things here, as they are trivial.

    In my AppDelegate didFinishLaunchingWithOptions I added this:

    let flutterViewController = FlutterViewController()
        self.navigationController = UINavigationController(rootViewController: flutterViewController)
        self.navigationController?.isNavigationBarHidden = true
    
        self.window = UIWindow(frame: UIScreen.main.bounds)
        self.window.rootViewController = self.navigationController
        self.window.makeKeyAndVisible()
    

    And my open method like this:

    private func openNativeUI(result: FlutterResult) {
            let sb = UIStoryboard(name: "Main", bundle: nil)
            let uiController = sb.instantiateViewController(withIdentifier: "nativeui")
    
            self.navigationController?.pushViewController(uiController, animated: true)
    
            result(true)
        }
    

    What do you think?

    Any advise is highly appreciated and thank you for your help in advance!