Xamarin.iOS view controller in Xamarin.Forms app

Let’s imagine that we have Xamarin.Forms app and for some reason we have to add Xamarin.iOS native objects like storyboards, xib or just want to go native for one or more views instead of creating Custom Renderers, Behaviors or Effects. Xamarin allows us to switch between Xamarin.Forms and Xamarin Native whenever we want. In this post I will show you how to do this in iOS app.

At the beginning we need to have Xamarin.Forms app, I already prepared one so please download it from here before we start connect Xamarin.iOS to existing Xamarin.Forms app.

Prepared Xamarin.Forms app is simple application with navigation between pages containing elements such as labels and buttons.

The challenge here is to add redirect to view controller in Xamarin.Forms navigation. Let’s start with adding new storyboard and view controller. To do this, add new file Main.Storyboard to Xamarin.iOS project.

In storyboard add View Controller, set it name to MainViewController to generate code behind and add label with text NativePage and button to go back to Xamarin.Forms part.

To obtain connection between native and forms part we have to use DependencyService, more about it you can find here. Let’s create interface for navigation between native and forms by adding new file with name INativeNavigateService in Xamarin.Forms project and then add below code.

public interface INativeNavigateService
{
    void Navigate();
}

Our interface will have only one method called Navigate that will navigate to initial view controller of Main storyboard.

Now we have to add platform implementation for created interface, to do this add NativeNavigateService to Xamarin.iOS project and add below code.

[assembly: Dependency(typeof(NativeNavigateService))]
namespace XamarinFormsSampleApp.iOS
{
    public class NativeNavigateService : INativeNavigateService
    {
        const string MainStoryboardName = "Main";

        public void Navigate()
        {
            var storyboard = UIStoryboard.FromName(MainStoryboardName, null);
            var mainViewController = storyboard.InstantiateInitialViewController();

            var window = UIApplication.SharedApplication.KeyWindow;
            window.RootViewController.PresentViewController(mainViewController, false, null);
        }
    }
}

In Navigate method we retrieve initial view controller from Main Storyboard and then present it on window as root view controller.

When you run application you will see that now we can go from Xamarin.Forms to Xamarin.iOS part with navigation.

One more thing left. We have to add return to Xamarin.Forms when user click button on view controller. To do this go to storyboard, select button, switch to events and add TouchUpInsideEvent, then add code below to created method.

partial void GoBack(UIButton sender)
{
    this.DismissViewController(false, null);
}

Now we can fully navigate between parts.

So we achieved our goal and now Xamarin.Forms application can navigate between Xamarin.Forms and Xamarin.iOS. If you want to see full solution you can get complete code from github. I hope that this post help you connect native and forms, but I personally recommend you to use Xamarin.Forms way with Custom Renderers, Behaviors or Events. It’s much easier, cleaner and doesn’t require so many changes that we done in this case. Of course in some cases this is the most reasonable way to go.

How about your experience with navigation between native and forms? Have you ever considered doing it the way I presented? If you have any knowledge in this topic, let me know in the comment!