Reuse and Reimplement the Xamarin.Forms Pages with iOS Extensions

As we all are now very well familiar with the basic nature and the most exciting factor of the Xamarin tech stage that it allows the developer’s community to re-use the same code base they once created in another developing application, but, do you know, apart from the code reuse facility, this wonderful cross-platform technology is now also allowing you to re-use the Xamarin.Forms pages in an iOS extension. These iOS extensions permit the developers to update and customize the present behavior of the application by simply implementing a few extra and advanced functionalities into the application in iOS and macOS extension points. Like the custom context actions, autofill password, incoming call filters. Besides that, the actions like content modifiers notification for Xamarin.iOS also supports the extensions. Thus, with the source of this blog we are taking you to the way where you can hassle-freely create an iOS extension with the Xamarin tools.

iOS extensions:

This is shared as a part of a pot application and is activated from a fixed extension point in the main application. These pot apps are basically a simple iOS app which usually provides a user with advice about the extension, along with answer to the basic queries, like how to activate and use it within an application.

Well, these 3 approaches can help the developer to share the code base of the main application between the extension and the pot app:

  1. The familiar iOS project

What the code that you want to share, simply place it between the pot (container) app and the extension into the shared iOS library. After that, reference the shared library in the two projects. Keep remembering that this shared library contains a native UIViewControllers from a Xamarin.iOS library.

  • The page or File Links:

In a few cases, the container applications are trusting to deliver more functionality, which is all due to the extensions which require to render a single “UIViewController”. Hence, in that few codes files is just natural to add a file link to the extension app. This process can be easily done from the file which is located in the container app.

  • Common Xamarin.Forms projects

In the Xamarin.Forms framework the app pages already been shared with another app development platform rather than iOS, like Android which requires the reimplement of the pages natively in the extension project. This happens just due to the iOS extension, which works well with the native UIViewControllers, but not with the Xamarin.Forms pages.

Besides that the above mentioned 3 steps this process a needs a few more steps to be done fairly, which are mentioned as:

Adition of Xamarin.Forms in an iOS extension project

The capacity to use a Xamarin.Forms in a native app development project are provided by the Native Forms. As it allows the developers to ContentPage as a derived page to be sum directly into the Xamarin.iOS native projects. While the method of CreateViewController extension exchanges example of a well-performing Xamarin.Forms pages to a native UIViewController. The more exciting thing about this, as it can be easily used or customized as a regular format controller, by simply using a similar approach in the iOS extension and the native iOS project.

While to reuse the iOS extension in the Xamarin.Forms in a simpler way, just use the mentioned 6 steps in your project and you are done.

1. Create a fresh Xamarin.Forms project and then modify your MainPage.XAML file.

2. Use the latest version of VS, take a visual studio template, and then add a new Xamarin.iOS project in it.

3. Now as the shape of the third step add the Xamarin.forms in the shared library of the extension project.

4. Now simply edit the entry point of the app to functionalize the Xamarin.Forms app and create your app. By doing this always keep in mind that the iOS needs an extension must define the app entry point in info.plist as NSExtensionMainStoryBoard or NSExtensionPrincipalClass.

5. Once you are successfully done with the activation of the entry point in your project, it’s the time for you to create an example of Xamarin.Forms page in order to show it to the users.

6. While in order to complete the process, open the entry point of your app, and then replace the ViewDidLoad method from the page with the following code:

public override void ViewDidLoad()

{

    base.ViewDidLoad();

    // Initialize Xamarin.Forms framework

    global::Xamarin.Forms.Forms.Init();

    // Create an instance of XF page with associated View Model

    var xfPage = new MainPage();

    var viewModel = (MainPageViewModel)xfPage.BindingContext;

    viewModel.Message = “Welcome to XF Page created from an iOS Extension”;

    // Override the behavior to complete the execution of the Extension when a user press the button

    viewModel.DoCommand = new Command(() => DoneClicked(this));

    // Convert XF page to a native UIViewController which can be consumed by the iOS Extension

    var newController = xfPage.CreateViewController();

    // Make sure the presentation style is set to full screen to avoid rendering the original entry point

    newController.ModalPresentationStyle = UIModalPresentationStyle.FullScreen;

    // Present new view controller as a regular view controller

    this.PresentModalViewController(newController, false);

}

Conclusion:

Whenever you have finely implemented the code and successfully done all the required settings in your project, this is the time for you to activate it. For this press, the share button icon at the bottom of the page, see the list of available extensions for activation action. From this list chose the MyAction extension. Once this extension is activated then the Xamarin.Forms page will successfully be displayed to the user. Remember that all the bindings and commands of the app will work in the container app.

0 0 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments