Integrating AppGallery Connect Crash Service in a Xamarin app for iOS
Tags: huawei, xamarin, ios, appgallery
Published: 2022-01-06 10:27:06

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](https://docs.microsoft.com/en-us/xamarin/cross-platform/get-started/requirements).

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d13ejuo2n9jww9vejlyo.png)

[Create a project](https://developer.huawei.com/consumer/en/doc/development/AppGallery-connect-Guides/agc-get-started-ios-0000001058442274) in AppGallery Connect and [enable HUAWEI Analytics](https://developer.huawei.com/consumer/en/doc/development/HMSCore-Guides/service-enabling-0000001050745155).

Right-click your project and choose **Manage NuGet Packages**.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gjjyowgxarsafaq6uf59.png)
Find the **Huawei.Agconnect.iOS.Crash** package and install it.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iptjq7wtgoc5n4wv5yh4.png)

Find the **Huawei.Agconnect.iOS.AgconnectCore** package, select the **1.2.0.300** version, and install it. 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bskejqzw26i3ilq84yhk.png)
 
Add the plist file from your AppGallery Project to the project directory.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/owfuw2mle9nnth2sx81n.png)

Set **Build Action** to **BundleResource**.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z6pclrya0fxduipk6zwg.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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gytdfw0wxh1x15xyl93u.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:

{% gist https://gist.github.com/devwithzachary/be48406ca5f3425142bb66eb1070a402 %}

## View Crash Report

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

View the crash statistics. 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a519zlmwwcgdsmrj1k9u.png)

Check the exceptions
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t1zbj7x2p4fxwvgc9x7y.png)

Diagnose the causes of exceptions.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4944sg3n1msssneon3qe.png)

Check the custom key-value pairs.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uqyjm15j0k78syg5au9j.png)
 
View the custom log levels
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ss3lir4axnjrbsmty9a2.png)

View custom user IDs. 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jtvarfml15a8zt7s4cte.png)

For details, please refer to:

[Crash for iOS](https://developer.huawei.com/consumer/en/doc/development/AppGallery-connect-Guides/agc-crash-introduction-0000001055732708)
[Codelab of Crash for iOS](https://developer.huawei.com/consumer/en/codelabsPortal/carddetails/CrashService-iOS)