Integrating AppGallery Connect Crash Service in a Xamarin app for iOS

Search This thread

devwithzachary

Inactive Recognized Developer
Oct 12, 2009
2,934
1,899
31
London
Huawei P40
The Huawei AppGallery Connect Crash service is a free to use crash analytics service which is supported on a wide range of platforms and frameworks.

Today lets take a look at how we can use in in a Xamarin, a hybrid framework and specifically set it up to work on iOS devices. This guide will start with a blank application but of course you can just as easily follow along and integrate into a pre-existing application!

Preparing the Xamarin Environment and Configuring Your Project​

You need to install Visual Studio for MAC first, and then select Mobile development with .NET in Visual Studio to install the Xamarin environment.


0*4h4E6lJFe1Et9cI4.png

Create a project in AppGallery Connect and enable HUAWEI Analytics.

Right-click your project and choose Manage NuGet Packages.


0*04_7A7mTv_fwsx7J.png

Find the Huawei.Agconnect.iOS.Crash package and install it.


0*poxLI6Yx_ibY9kc-.png

Find the Huawei.Agconnect.iOS.AgconnectCore package, select the 1.2.0.300 version, and install it.


0*M30TPMxVZUxXuhGb.png

Add the plist file from your AppGallery Project to the project directory.


0*AX128pMPgk3QNILn.png

Set Build Action to BundleResource.


0*kLinkVFtPRs83IPL.png

Developing Your App​

Start by Configure the layout of your app for the testing of the crash service, in this example we will create three buttons to trigger different tests.
Double-click main.storyboard to launch Xcode, and create the MakeCrash, CatchException, and CustomReport buttons


0*RjAJ_6vMVoPsXYnR.png

In the MainActivity.cs file, call AGConnectCrash.Instance.TestIt to trigger a crash, call AGConnectCrash.Instance.SetUserId to set a custom user ID, call AGConnectCrash.Instance.SetCustomKey to set the key and value for a custom key-value pair, call AGConnectCrash.Instance.Log to set the log level, and call AGConnectCrash.Instance.RecordException to record a non-fatal exception.

This could look something like:
Code:
using System;
using UIKit;
using Huawei.Agconnect.Crash;
using Foundation;


namespace crashios0512
{
    public partial class ViewController : UIViewController
    {
        public ViewController(IntPtr handle) : base(handle)
        {
        }

        public override void ViewDidLoad()
        {
            base.ViewDidLoad();
            // You can configure additional functions as required.
        }

        public override void DidReceiveMemoryWarning()
        {
            base.DidReceiveMemoryWarning();
            // Delete the cached data or images that are no longer used.
        }


        partial void MakeCrash(UIKit.UIButton sender)
        {
            AGCCrash.GetSharedInstance().TestIt();
        }

        partial void CatchException(UIKit.UIButton sender)
        {
            AGCCrash.GetSharedInstance().RecordError(new Foundation.NSError());
        }

        partial void CustomReport(UIKit.UIButton sender)
        {
            AGCCrash.GetSharedInstance().SetUserId("testuser");
            AGCCrash.GetSharedInstance().Log("default info level");
            AGCCrash.GetSharedInstance().SetCustomValue(new NSString("test"), "this is string value");
            AGCCrash.GetSharedInstance().LogWithLevel(AGCCrashLogLevel.Warning, "this is warning log level");
            AGCCrash.GetSharedInstance().SetCustomValue(new NSNumber(123), "this is number");
        
        }
    }
}

View Crash Report​

Tap MakeCrash, CatchException, and CustomReport in sequence, and check the report in AppGallery Connect.

View the crash statistics.


0*f8RZXsC7xMt1AjoL.png

Check the exceptions


0*f7cPCqVCFAj0pi7u.png

Diagnose the causes of exceptions.


0*6expZmG5owenWWli.png

Check the custom key-value pairs.


0*NxVGgh6HGn7l7U7J.png

View the custom log levels


0*QVfxl8Rny12IOu9v.png

View custom user IDs.


0*5HgJmz9EX4FJ8jG_.png

For details, please refer to:

Crash for iOS
Codelab of Crash for iOS