[Huawei Developers] Freemind R:
More articles like this, you can visit HUAWEI Developer Forum

​

HUAWEI Analytics Kit 5.0 version has been updated, and it supports iOS SDK now, enabling one-stop, unified analysis of cross-platform Android and iOS users. It looks into user-centric analysis and helps you understand user behavior on the Android and iOS platforms. Today I want to share with you how to add Analytics Kit to your iOS App. The version I integrated is 5.0.0.300.

The general steps we will walk through include:

Configuring App Information in AppGallery Connect

Integrating the HMS Core SDK

Accessing HUAWEI Analytics Kit

Configuring App Information in AppGallery Connect

Follow the steps below to configure App Information in AppGallery Connect for your iOS application. If you have both an Android and an iOS version of your app, you can set them within the same project, this will allow you to finish the data analysis conveniently.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/024/107/332/5190376000024107332.20200707084126.80380597706514586023591051252125:50510714094810:2800:34A638966824DA0DE63C7E4516263F5E49A16914C303600BCEC05C7C9A990880.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/024/107/332/5190376000024107332.20200707084126.80380597706514586023591051252125:50510714094810:2800:34A638966824DA0DE63C7E4516263F5E49A16914C303600BCEC05C7C9A990880.png[/IMG]

Getting Started:

1. Have your Bundle ID ready for your iOS app (ask your developers for this).

2. Go to AppGallery Connect and create a new project.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/024/107/332/5190376000024107332.20200707084227.82688737406132802727222427228907:50510714094810:2800:1DD4BB907E61968DD95860840B41D891110330B1AEC62C574CA29321C15E9FF7.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/024/107/332/5190376000024107332.20200707084227.82688737406132802727222427228907:50510714094810:2800:1DD4BB907E61968DD95860840B41D891110330B1AEC62C574CA29321C15E9FF7.png[/IMG]

Once you’ve created a new project, you’ll see a page, and the side navigation bar shows the AGC's various function menus.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/024/107/332/5190376000024107332.20200707084313.74662036616077376066050562173700:50510714094810:2800:ED6C1292D813DEE254AF2D8168112C5AFA6A1AB90BA3EAC966C488E8ED187500.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/024/107/332/5190376000024107332.20200707084313.74662036616077376066050562173700:50510714094810:2800:ED6C1292D813DEE254AF2D8168112C5AFA6A1AB90BA3EAC966C488E8ED187500.png[/IMG]

3. Click Add app, and create an iOS application.

Here the App package ID is the Bundle ID.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/024/107/332/5190376000024107332.20200707084416.94528072139088841191668656977527:50510714094810:2800:65DAED26BB7904AABE03BE4C6098F325F5761981FF1CCF05FAF2DF9A6C0BB999.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/024/107/332/5190376000024107332.20200707084416.94528072139088841191668656977527:50510714094810:2800:65DAED26BB7904AABE03BE4C6098F325F5761981FF1CCF05FAF2DF9A6C0BB999.png[/IMG]

Click OK, and then you will get the configuration file agconnect-services.plist.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/024/107/332/5190376000024107332.20200707084455.03289254543972080218201394515881:50510714094810:2800:AAC95FF5767E2893BA8EC90A7B62C41C08EEE8C89316894B7EC32CF05A4D190F.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/024/107/332/5190376000024107332.20200707084455.03289254543972080218201394515881:50510714094810:2800:AAC95FF5767E2893BA8EC90A7B62C41C08EEE8C89316894B7EC32CF05A4D190F.png[/IMG]

Click Manage APIs, and here we’ve already had the Analytics API enabled.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/024/107/332/5190376000024107332.20200707084540.69303753036477563904006544117541:50510714094810:2800:2D318BB73BCE04B9902BBFB4227C12FDA6715747F595554BB9BA59B43AB8FB59.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/024/107/332/5190376000024107332.20200707084540.69303753036477563904006544117541:50510714094810:2800:2D318BB73BCE04B9902BBFB4227C12FDA6715747F595554BB9BA59B43AB8FB59.png[/IMG]

Back to the side navigation bar, and go to the HUAWEI Analytics tab, you will see all kinds of functions, including User analysis, Behavior analysis, etc. Select any menu to Enable Analytics service.

On the Project access settings page, set the data storage location, time zone, currency, user data storage time, and calendar week, and click Finish.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/024/107/332/5190376000024107332.20200707084649.63547161612888651747512982470046:50510714094810:2800:1D273D208DF5E232C7490FAEC4304296695430AA736BFD530929E47C5302E513.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/024/107/332/5190376000024107332.20200707084649.63547161612888651747512982470046:50510714094810:2800:1D273D208DF5E232C7490FAEC4304296695430AA736BFD530929E47C5302E513.png[/IMG]

Integrating the HMS Core SDK (in Cocoapods Mode)

I prefer the Cocoapods Mode. You can also integrate the HMS Core SDK into the Xcode Project Manually.

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

a. Click Download agconnect-services.plist to obtain your iOS config file (agconnect-services.plist).

b. Move your agconnect-services.plist file into the root of your Xcode project.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/024/107/332/5190376000024107332.20200707085909.63445204238661648197460913953798:50510714095238:2800:9B21BC25D9F289E6A37F5C72FD3F1049B0EDD1DFEF163A51A963157F2D1043F6.jpg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/024/107/332/5190376000024107332.20200707085909.63445204238661648197460913953798:50510714095238:2800:9B21BC25D9F289E6A37F5C72FD3F1049B0EDD1DFEF163A51A963157F2D1043F6.jpg[/IMG]

2. Edit the Podfile file

a. Create a Podfile if you don't already have one:

$ cd your-project-directory

$ pod init

b. To your Podfile, add the pods that you want to use in your app: Add pod 'HiAnalytics', that is, the dependency for pod.

Example Podfile file:

# Uncomment the next line to define a global platform for your project
# platform :iOS, '9.0'
target 'HiAnalyticsSwiftDemo' do
# Comment the next line if you don't want to use dynamic frameworks
use_frameworks!
# Pods for HiAnalyticsSwiftDemo
pod 'HiAnalytics'
target 'HiAnalyticsSwiftDemoUITests' do
# Pods for testing
end
end

c. Install the pods, then open your .xcworkspace file to see the project in Xcode:

$ pod install

$ open your-project.xcworkspace

Accessing HUAWEI Analytics Kit

1. Initialize Analytics SDK in your app

The final step is to add initialization code to your application. Initialize AppDelegate through the config interface.

Objective C sample code: Perform initialization in AppDelegate.m.

#import "AppDelegate.h"
#import <HiAnalytics/HiAnalytics.h>

@interface AppDelegate ()

@end

@implementation AppDelegate
...
- (BOOL)Application:(UIApplication *)Application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after Application launch.
[HiAnalytics config];//Initialization
return YES;
}
...
@end

Swift code example: Perform initialization in AppDelegate.swift.

import UIKit
import HiAnalytics

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
...
func Application(_ Application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after Application launch.
HiAnalytics.config();//Initialization
return true
}
...
}

2. Record defined events using the onEvent API.

For definitions of the events, please refer to Event Description.

3. Call APIs of HUAWEI Analytics Kit to implement the corresponding functions. For details, please refer to API Reference.

During the development, you can use DebugView to view the event records in real time, observe the results, and adjust the event tracing scheme.

Enabling/Disabling the Debug Mode

1.To enable the debug mode:

Go to Product > Scheme > Edit Scheme from the Xcode menu. On the Arguments page, click + to add the -HADebugEnabled parameter. After the parameter is added, click Close to save the setting.

1. –HADebugEnabled

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/024/107/332/5190376000024107332.20200707090228.72318101946829256762298676594631:50510714095756:2800:1B1364E3C3B5D5EFE91C3264137168EA40FC5E0234EDD507D7B943DC909DB9E4.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/024/107/332/5190376000024107332.20200707090228.72318101946829256762298676594631:50510714095756:2800:1B1364E3C3B5D5EFE91C3264137168EA40FC5E0234EDD507D7B943DC909DB9E4.png[/IMG]

2.To disable the debug mode

1. -HADebugDisabled

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/024/107/332/5190376000024107332.20200707090350.75112256640705502882315241621690:50510714095756:2800:63F1F7F77C439C028EE770BB4ACFD814AA663F651E19442821D47659F98648AE.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/024/107/332/5190376000024107332.20200707090350.75112256640705502882315241621690:50510714095756:2800:63F1F7F77C439C028EE770BB4ACFD814AA663F651E19442821D47659F98648AE.png[/IMG]

After the data is successfully reported, you can go to HUAWEI App Debugging to view the data, as shown in the following figure.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/024/107/332/5190376000024107332.20200707090449.83522435746239039041341709708848:50510714095756:2800:9C013692CDA544D63A1A2F9A52E4AAB6EF87798CC5EE3FC0491E08352B590AB7.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/024/107/332/5190376000024107332.20200707090449.83522435746239039041341709708848:50510714095756:2800:9C013692CDA544D63A1A2F9A52E4AAB6EF87798CC5EE3FC0491E08352B590AB7.png[/IMG]

Q&A:

1. I have integrated the iOS SDK of HUAWEI Analytics Kit into my app but why no log events are collected when the app is running?

When this problem occurs, it is probable that the run log parameters have not been set. You can set the log level to any of the following values: -HALogLevelDebug, -HALogLevelInfo, -HALogLevelWarn, and -HALogLevelError.

For example, to set the log level to -HALogLevelDebug, perform the following steps:

Choose Product > Scheme > Edit Scheme from the Xcode menu.

On the Arguments page, click + to add the -HALogLevelDebug parameter.

After the parameter is added, click Close to save the setting.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/024/107/332/5190376000024107332.20200707090653.91936134638442656532625467654680:50510714095756:2800:524849AEACFD6676F943ECA8A590D8C43DD8FBD5CBBED9E48C75855000BA2B8B.jpg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/024/107/332/5190376000024107332.20200707090653.91936134638442656532625467654680:50510714095756:2800:524849AEACFD6676F943ECA8A590D8C43DD8FBD5CBBED9E48C75855000BA2B8B.jpg[/IMG]

Face problems during development? Take it easy. Go to Stack Overflow and raise your questions. Huawei experts will get back to you as soon as possible.

https://stackoverflow.com/questions/tagged/huawei-mobile-services?tab=Newest