Integrating Huawei's AppGallery Connect Auth Service for iOS
Tags: huawei, ios, appgallery, authentication
Published: 2021-12-10 09:19:40

Auth Service helps you quickly and easily develop account registration and sign-in functions for your app.

This service is offered completely free of charge and supports a range of platforms including Android and iOS!

Today we are going to take a look at how this can be quickly integrated into an iOS application.

## Enabling Auth Service

To enable Auth Service, sign in to [AppGallery Connect](https://developer.huawei.com/consumer/en/service/josp/agc/index.html#/), click **My projects**, click your project card, and select an app for SDK integration from the app drop-down list at the top of the page. Then go to Build > Auth Service, and enable the service if you haven't already.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q6gt5w8ke1cly9gxa3wt.png)
 
## Integrating the Service SDK
If you are using Xcode, you need to integrate the service SDK into your Xcode project using 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**.
1. Click your project card and select an app for SDK integration from the app drop-down list at the top of the page.
1. Go to **Project settings > General information** and download **agconnect-services.plist** under App information.
1. Copy the **agconnect-services.plist** file to your app's module directory.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/thsqezyztwebyufzgzq2.png)
 
### Create a PodFile
Open the CLI and navigate to the Xcode project. Then, create a Podfile. Skip this step if a Podfile already exists.

`cd project-directory 
pod init`

### Edit the PodFile
Integrate the service SDK.
Add the pod dependency AGConnectAuth to the Podfile.

`target 'AGConnectAuthDemo' do
pod 'AGConnectAuth'
end`

Install the pod and open the .xcworkspace file to view the project.

`pod install`

## Designing the UI

You can create a layout page in the Xcode project and design the UI based on the following figure, to allow users to register an account on your app using a mobile number or email address.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pz5wfnealzjtvwatidr2.png)
 
## Enabling Required Authentication Modes

1. Sign in to [AppGallery Connect](https://developer.huawei.com/consumer/en/service/josp/agc/index.html#/) and click **My projects**.
2. Click your project card and select an app from the app drop-down list at the top of the page.
3. Go to **Build > Auth Service**. On the **Authentication mode** tab page, enable **Mobile number** and **Email address** in the Operation column.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/un97iibw81mebh85zplf.png)

## Developing Functions

Associate Storyboard with ViewController so that you can obtain parameters required for sign-up and sign-in through text boxes

{% gist https://gist.github.com/devwithzachary/1c7ead4604cf8ff7dd3bcd69906710e2 %}

### Mobile Number

If the mobile number entered has not been used to register an account, you need to send a verification code to it to check whether it belongs to the user. Enter a mobile number and tap **Send verification code**. Then call the method for requesting a verification code and obtain the result in the callback.

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

Enter the verification code you received and the password set for your account. Tap the **register button**, call the method for mobile number registration, and obtain the result in the block callback.

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

After successfully registering an account, enter the mobile number and password you set, and tap **login** to sign in to your app. Congratulations, you have now successfully enabled registration and sign-in with a mobile number for your app.

{% gist https://gist.github.com/devwithzachary/73163d4a78bbc91e632c364065c15a78 %}

### Email
If the email address entered has not been used to register an account, you need to send a verification code to it. Enter the email address and tap Send verification code. Then call the method for requesting a verification code and obtain the result in the callback.

{% gist https://gist.github.com/devwithzachary/241880e1f7e4c41d628535cda5f29147 %}

Enter the verification code you received and the sign-in password, and tap register.

{% gist https://gist.github.com/devwithzachary/5aa0d76ec96dd1244c4333d7dcb77969 %}

Enter the email address and the password you set and tap login to sign in to your app. Congratulations, you have now successfully enabled registration and sign-in with an email address for your app.

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

## Summary
Auth Service is a service that provides you with a quick and easy way to develop registration and sign-in functions for app. Its lightweight SDK also help you reduce your app’s size. 

## Reference
More information about [Auth Service](https://developer.huawei.com/consumer/en/doc/development/AppGallery-connect-Guides/agc-auth-introduction-0000001053732605)