What Is the Crash Service of AppGallery Connect?

The Crash service of AppGallery Connect is a lightweight crash analysis service, in which Huawei provides a Crash SDK that can be quickly integrated into your app, without the need for coding. The SDK integrated into your app can automatically collect crash data and report the data to AppGallery Connect when your app crashes, helping you understand the version quality of your app, quickly locate the causes of crashes, and evaluate the impact scope of crashes.

What You Will Create

In this codelab, you will create an app that can integrate the Crash service and manually trigger a crash to test the Crash service.
You can also customize your crash report to suit your service by configuring custom user IDs, recording custom logs, and defining custom key-value pairs.

What You Will Learn

Development Environment Requirements

Device Requirements

To integrate the Crash service of AppGallery Connect, you must complete the following preparations:

For details, please refer to Getting Started with iOS.

Enabling HUAWEI Analytics

The Crash service uses capabilities of HUAWEI Analytics when reporting crash events. Therefore, you must enable HUAWEI Analytics before integrating the Crash SDK. For details, please refer to Service Enabling.

Integrating the Crash SDK

If you are using Xcode, you need to integrate the Crash SDK into your Xcode project with CocoaPods.

1. Add the AppGallery Connect configuration file of the app to your Xcode project.

  1. Sign in to AppGallery Connect and click My projects.
  2. Find your project from the project list and click the app for SDK integration on the project card.
  3. Go to Project settings > General information, and click agconnect-services.plist under App information to download the configuration file.
  4. Copy the agconnect-services.plist file to the app's root directory of your Xcode project.

2. Create a Podfile.

Open the CLI and navigate to the location of the Xcode project. Then, create a Podfile. Skip this step if a Podfile already exists.

cd project-directory pod init

3. Edit the Podfile.

  1. Integrate HUAWEI Analytics and the Crash SDK.
    Edit the Podfile to add pod dependencies of HUAWEI Analytics and the Crash SDK.
    target 'ioscrashdemo' do # Pods for ioscrashdemo pod 'HiAnalytics' pod 'AGConnectCrash' end end
  2. Install the pod and open the .xcworkspace file to view the project.
    pod install

You can create a page in your Xcode project and design the UI according to the following figure, with two buttons required: One button is used to trigger crashes and the other is used to trigger the generation of a custom report.

In this codelab, you can test the Crash service by calling the API of the Crash SDK to create a crash and custom report, and then check whether the crash data and custom report are reported in AppGallery Connect. The procedure is as follows:

  1. Initialize the AppGallery Connect SDK and Analytics SDK. Import header files to the AppDelegate.m file of the project and add code for initialization.
    #import "AppDelegate.h" #import <AGConnectCore/AGConnectCore.h> #import <HiAnalytics/HiAnalytics.h> @implementation AppDelegate - (BOOL)Application:(UIApplication *)Application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after Application launch. [AGCInstance startup]; [HiAnalytics config]; return YES; } ... @end
  2. Create a test button Make Crash in ViewController of your app. You can tap the button to call testIt to trigger a crash event.
    #import "ViewController.h" #import <AGConnectCrash/AGConnectCrash.h> @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRectMake(100, 200, 100, 40); [button setTitle:@"Make Crash" forState:UIControlStateNormal]; [button addTarget:self action:@selector(makecrash) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button]; } - (void)makecrash { [[AGCCrash sharedInstance] testIt]; } ... @end
  3. Create a test button Custom Report in ViewController of your app. You can tap the button to call the setUserId: method to set the user ID, the logWithLevel:message: method to record logs, and the setCustomValue:forKey: method to add custom key-value pairs.
    #import "ViewController.h" #import <AGConnectCrash/AGConnectCrash.h> @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRectMake(100, 300, 100, 40); [button setTitle:@"Custom Report" forState:UIControlStateNormal]; [button addTarget:self action:@selector(customreport) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button]; } - (void) customreport { [[AGCCrash sharedInstance] setUserId:@"iosuser"]; [[AGCCrash sharedInstance] log:@"This is default log."]; [[AGCCrash sharedInstance] logWithLevel:AGCCrashLogLevelDebug message:@"This is debug log."]; [[AGCCrash sharedInstance] logWithLevel:AGCCrashLogLevelInfo message:@"This is info log."]; [[AGCCrash sharedInstance] logWithLevel:AGCCrashLogLevelWarning message:@"This is warning log."]; [[AGCCrash sharedInstance] logWithLevel:AGCCrashLogLevelError message:@"This is error log."]; [[AGCCrash sharedInstance] setCustomValue:@"Hello World" forKey:@"This is a World"]; [[AGCCrash sharedInstance] setCustomValue:@(1234.567) forKey:@"This is a number"]; } ... @end
  4. Package and run the app. Then tap Custom Report to generate a custom crash report and tap Make Crash to make a crash.
  1. Sign in to AppGallery Connect and click My projects.
  2. Find your project from the project list and click the app on the project card.
  3. Go to Quality > Crash. On the Crash page, click the Statistics tab and select Last hour to view crash statistics of your app. You can trigger multiple crashes and view the real-time crash statistics in AppGallery Connect.
  4. If crash data was reported, click the Problems tab, select Last hour, and find the crash of your app. For example, in the following figure, NSRangeException is the crash triggered during the test.
  5. Click the crash name to go to the crash details page. Click the Stack tab. The possible causes of the crash are displayed, helping you analyze the crash.
  1. Click the Search by user tab and check the crash report by user ID.
  2. Click a crash to view its details. On the details page that is displayed, you can click the Logs tab to check generated custom logs.
  3. Click the Status tab to view information about custom key-value pairs.

You have successfully created an app integrated with the Crash service of AppGallery Connect and learned how to view and analyze crashes and custom crash reports in AppGallery Connect. You can trigger different types of crashes and view the crashes in AppGallery Connect.

For details about APIs related to the Crash service of AppGallery Connect, please refer to API Reference.

To download the sample code, please click the button below:

Download

Code copied