Implementing the Good Dynamics SDK with Xamarin.Forms on iOS

Implementing the Good Dynamics SDK with Xamarin.Forms on iOS

The app delegate is an important piece of any iOS application that serves as a communication mechanism between the application’s code and the iOS device’s application management system.  The app delegate is important to iOS developers because it gives them the ability to control what the app does based on the app’s state on the device.  For example, the app delegate knows when the app is active, minimized, about to start up, finished starting up, closed, etc. which effectively makes it the root object of the iOS app.  In every Xamarin iOS application, the app delegate is exposed through the AppDelegate class.

Many different frameworks and SDKs come with their own version of the AppDelegate that require the developer’s AppDelegate class to subclass.  Because the C# language does not support multiple inheritance, Xamarin’s AppDelegate class can only subclass one AppDelegate.  A conflict arises when the developer wants to utilize more than one AppDelegate because two or more frameworks require it.

This blog post serves to provide a work-around to address this conflict.  Using Xamarin.Forms and the Good Dynamics SDK as examples, both of these frameworks have their own AppDelegate to subclass.  Xamarin.Forms is a cross platform UI toolkit that allows developers to create native user interfaces across iOS, Android and Windows Phone.  The Good Dynamics SDK allows iOS apps to utilize Good’s security, infrastructure and backend services.  The two app delegates in question are Good’s GDiOSDelegate and Xamarin.Forms’s FormsApplicationDelegate.

Now, onto the work-around; select one AppDelegate to subclass your application’s AppDelegate from.  For this example, lets subclass FormsApplicationDelegate and perform the work-around with GDiOSDelegate.  The general idea is to make a class that inherits from GDiOSDelegate and use EventHandlers to call into the methods of the GDiOSDelegate.  From there, hook up those EventHandlers to the appropriate methods of the AppDelegate.

Below is the code for the class that inherits from the GDiOSDelegate.  In this case we are only providing functionality for authorization.

public class MyGDDelegate : GDiOSDelegate
{
    public EventHandler<GDAppEvent> OnAuthorized;
    public EventHandler<GDAppEvent> OnNotAuthorized;

    public override void HandleEvent(GDAppEvent anEvent)
    {
        switch (anEvent.Type)
        {
            case GDAppEventType.Authorized:
               OnAuthorized(this, anEvent);
               break;
            case GDAppEventType.NotAuthorized:
               OnNotAuthorized(this, anEvent);
               break;
        }
    }
}

Next, do your initialization of the GDiOSLibrary in the AppDelegate as you would with any Good application.  The difference here is that instead of setting GDLibrary.Delegate to “this” (which would normally work if AppDelegate inherited from GDiOSDelegate), use the class you just created above.  In this case, it’s “MyGDDelegate”.

After that, hook up the AppDelegate’s OnAuthorized and OnNotAuthorized methods to MyGDDelegate’s appropriate EventHandlers.  Make sure to include GDAppEvent as a parameter for each of these methods, as your method signatures must match between the different AppDelegates.

All that’s left is to set the GDLibrary’s Delegate property to the instance of MyGDDelegate.

public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
{
    public GDiOS GDLibrary { get; private set; }

    public override bool FinishedLaunching (UIApplication app, NSDictionary options)
    {
        GDLibrary = GDiOS.SharedInstance();
        MyGDDelegate del = new MyGDDelegate ();
        del.OnAuthorized += OnAuthorized;
        del.OnNotAuthorized += OnNotAuthorized;

        GDLibrary.Delegate = del;
        GDLibrary.Authorize ();

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

        LoadApplication (new App ());

        return base.FinishedLaunching (app, options);
    }

    private void OnAuthorized(object sender, GDAppEvent anEvent)
    {
 
    }

    private void OnNotAuthorized(object sender, GDAppEvent anEvent)
    {
 
    }
}

 

When GDLibrary.Authorize fires, it will attempt to call its delegate’s OnAuthorized or OnNotAuthorized methods.  Because we are using EventHandlers on our MyGDDelegate class, it calls into the class’s HandleEvent method which calls its parent’s (GDiOSDelegate) appropriate method.  The OnAuthorized and OnNotAuthorized methods on the AppDelegate are called after MyGDDelegate completes its event.

If you want to use more functionality of the GDiOSDelegate, you will have to add an EventHandler to MyGDDelegate then hook it up to the AppDelegates appropriate method if you want both to fire.  Calling the MyGDDelegate method itself just requires you to use GDLibrary.Delegate.  This completes the work-around.

Using this work-around not only solves the Xamarin.Forms & Good Dynamics SDK problem, but it can also be applied to any scenario where you need to utilize the custom AppDelegates of multiple frameworks in your Xamarin iOS app.

Our mobility consulting professionals have extensive experience developing enterprise and consumer mobile apps using Xamarin on the Apple iOS, Android, and Windows Phone platforms. We’ve built numerous apps utilizing Good Dynamics.  Contact us for more information on how we can work with you to build highly secure, cross-platform mobile applications.

2 Comments

  • Martin Brekhof May 27, 2016 3:19 am

    A similar blogpost but now featuring Android (including using a web service API to retrieve data) would be much appreciated.

    • Lindsay Croke May 27, 2016 8:52 am

      Hi Martin, thanks for visiting our blog and for the feedback! We’ll be sure to publish a follow-up about web service API in the near future.

Your email address will not be published. Required fields are marked *

Phone: 312-602-4000
Email: marketing@westmonroepartners.com
222 W. Adams
Chicago, IL 60606
Show Buttons
Share On Facebook
Share On Twitter
Share on LinkedIn
Hide Buttons