iOS extensions allow developers to customize existing system behaviors by adding extra functionality to iOS and macOS Extension Points. Such as custom context actions, password autofill, and incoming calls filters. Even actions like notification content modifiers. Xamarin.iOS supports extensions and this awesome guide will walk you through creating an iOS extension using Xamarin tools.
Extensions are distributed as part of a Container app and activated from a specific Extension Point in a Host app. The Container app is usually a simple iOS application. It provides a user with information about the Extension. As well as how to activate and how to use it. There are three main approaches to sharing code between an Extension and a Container app:
UIViewController
. With few files to share, it is common to add a file link to the Extension app. This is done from the file located in the Container app.Additional steps are needed to use Xamarin.Forms in the iOS Extension. These steps are explained below.
The ability to use Xamarin.Forms in a native project is provided via Native Forms. It allows ContentPage
-derived pages to be added directly to native Xamarin.iOS projects. The CreateViewController
extension method converts an instance of a Xamarin.Forms page to a native UIViewController
. This can then be used or modified as a regular controller. Use the same approach with an iOS Extension as a special kind of a native iOS project.
Important! There are many known limitations for iOS Extensions. Although you can use Xamarin.Forms, do so very carefully. Monitor memory usage and startup time. Otherwise, the Extension could be terminated by iOS without a graceful way to handle it.
NSExtensionMainStoryboard
or NSExtensionPrincipalClass
.ActionViewController.ViewDidLoad
method), create an instance of a Xamarin.Forms page and show it to a user.ViewDidLoad
method with the following implementation: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);
}
#developers #ios #xamarin #xamarin.forms #xaml #ios extension #native forms