Overview

What You Will Create

What You Will Learn

Development Environment and Skill Requirements

Device Requirements

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

  1. Sign in to AppGallery Connect, click My projects, click your project card, and select an app for which you want to enable the Crash service from the drop-down list on the top.
  2. Go to Quality > Crash. If you use the Crash service for the first time, click Enable now to enable HUAWEI Analytics first.

You need to integrate the Crash service SDK into your Xcode project with CocoaPods.

  1. Sign in to AppGallery Connect and click My projects.
  2. Click your project card and select an app for SDK integration from the app drop-down list on the top.
  3. Go to Project settings > General information. In the App information area, download the agconnect-services.plist file.
  4. Copy the agconnect-services.plist file to your project.
  5. 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
  6. Add the pod ‘AGConnectCrash' dependency to the Podfile.
    target 'AGC-Crash-2' do pod 'AGConnectCrash' end
  7. Install the pod and open the .xcworkspace file to view the project.
    pod install
  8. The following figure shows the result after installation.

You can create a page layout in your iOS project and design the UI according to the following figure, with a button required for making a crash.

Sample code:

override func viewDidLoad() { super.viewDidLoad() let crashButton = UIButton(frame: CGRect(x: 50, y: 80, width: 160, height: 50)) crashButton.backgroundColor = UIColor.blue crashButton.setTitle("Make Crash", for: .normal) crashButton.addTarget(self, action: #selector(makeCrash), for: .touchUpInside) self.view.addSubview(crashButton) let customReportButton = UIButton(frame: CGRect(x: 50, y: 160, width: 160, height: 50)) customReportButton.backgroundColor = UIColor.blue customReportButton.setTitle("Custom Report", for: .normal) customReportButton.addTarget(self, action: #selector(customReport), for: .touchUpInside) self.view.addSubview(customReportButton) }
  1. Import AGConnectCore to the AppDelegate class of the app, and call AGCInstance.startUp in the didFinishLaunchingWithOptions method for initialization.
    import AGConnectCore func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { AGCInstance.startUp() }
  2. Call the method of making a crash in the Make Crash tap event.
    @objc func makeCrash() { AGCCrash.sharedInstance().testIt() }
  3. In the Custom Report tap event, call AGCCrash.sharedInstance().setUserId to set a user ID, call AGCCrash.sharedInstance().log to set logs, and call AGCCrash.sharedInstance().setCustomValue to set key-value pairs.
    @objc func customReport() { AGCCrash.sharedInstance().setUserId("testuser") AGCCrash.sharedInstance().log(level: .debug, message: "set debug log") AGCCrash.sharedInstance().log(level: .info, message: "set info log") AGCCrash.sharedInstance().log(level: .warning, message: "set warning log") AGCCrash.sharedInstance().log(level: .error, message: "set error log") AGCCrash.sharedInstance().setCustomValue(value: "Hello world", key: "stringKey") AGCCrash.sharedInstance().setCustomValue(value: false, key: "booleanKey") AGCCrash.sharedInstance().setCustomValue(value: 1.1, key: "doubleKey") AGCCrash.sharedInstance().setCustomValue(value: 1.1, key: "floatKey") AGCCrash.sharedInstance().setCustomValue(value: 0, key: "intKey") AGCCrash.sharedInstance().setCustomValue(value: 11, key: "longKey") }
  1. Click the Xcode run button to run your app on a mobile phone or simulator.
  2. Click Custom Report to customize your crash report.
  3. The crash data collection function is not enabled in debugging mode in Xcode. Therefore, before testing the crash data collection function of your app, stop debugging in Xcode, open the app, and tap Make Crash.
  4. After a crash occurs, ensure that the network connection is normal, restart the app, and view crash information in AppGallery Connect.
  1. Sign in to AppGallery Connect, click My projects, click the project card, and select your app.
  2. On the Crash service page, click the Statistics tab to view the crash statistics of your app.
  3. Click Search by user, and find the custom user ID.
  4. 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.
  5. 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 also create your crash report in a required form, and check it in AppGallery Connect.

API Reference

Sample code

Code copied