introduce
How to let the user according to the existing account to log in to register? The APPLICATION integrates the HUAWEI AGC authentication service SDK to realize this function quickly and easily.
This article summarizes the integration process according to the official website documents. There are a lot of information on the official website about the integration steps. Now I summarize the steps:
1. Open Huawei certification service
2. Integrate SDK into iOS projects
3. Design the login registration page
4. Enable authentication
5. Registration and login function development
6. Pack test
Enable huawei authentication services
From the AppGallery Connect page, click “My Project”, select the application that needs to be certified from the list of apps in the project, and navigate to “Build > Certification Services”. If you are using the authentication service for the first time, please click “Open now” to open the service.
To integrate the SDK
For your Xcode development environment, you can integrate the certification SDK into your Xcode development environment through the Cocoapods integration.
1. Add the AppGallery Connect profile for the current application to the Xcode project.
-
Log on to AppGallery Connect and click “My Project”.
-
Locate your project in the project list, and select the application that you want to integrate with the SDK from the list of apps under the project.
-
Click “Agconnect-services. plist” under “Application” in “Project Settings” to download the configuration file.
-
Copy the agconnect-services.plist file to the application-level root directory.
2. Create a Podfile.
Open a command line window, navigate to the location of the Xcode project, and create the Podfile. If yes, skip this step.
cd project-directory
pod init
Copy the code
3. Edit the Podfile file.
- Integration Authentication SDK
Edit the Podfile file to add pod dependencies on POD ‘ ‘AGConnectAuth’.
target 'AGConnectAuthDemo' do
pod 'AGConnectAuth'
end
Copy the code
- Execute Pod Install, then open the.xcWorkspace file to view the project.
Interface design
In this Codelab, you can create a layout page in Xcode project, refer to the following figure for UI design, and register and log in by mobile phone number and email account.
Enabling authentication Mode
1. Log in to AppGallery Connect and click “My Project”.
2. Find your project in the project list and select your application from the application list.
3. In the Build column, select Authentication Service. On the Authentication Mode TAB page, click Operation, and enable mobile phone number and email address.
Register login function development
1. Associate the storyboard controls to the ViewController so that you can use the input field to get the parameters required for login registration.
@IBOutlet weak var phoneText: UITextField! @ibOutlet Weak var phoneVertifyText: UITextField! // Mobile verification code input box @ibOutlet weak var phonePassword: UITextField! @ibOutlet Weak var emailText: UITextField! @ibOutlet Weak var emailVertifyText: UITextField! @ibOutlet weak var emailPassword: UITextField! // Email account password input boxCopy the code
2. If you use an unregistered mobile phone number, you need to send a verification code to your mobile phone to verify your registration. Input mobile phone number, click “send verification code” button, the code is executed to the method of requesting mobile phone verification code, you can see the result of sending in the return.
@IBAction func phoneSendVertifyCode(_ sender: Any) { let setting = AGCVerifyCodeSettings.init(action: AGCVerifyCodeAction.registerLogin, locale: nil, sendInterval: 30) AGCPhoneAuthProvider.requestVerifyCode(withCountryCode: "86", phoneNumber: phoneText.text ?? "", Settings: setting). OnSuccess {(results) in // Mobile verification code sent successfully}. OnFailure {(error) in // mobile verification code sent failed}}Copy the code
3. After receiving the verification code, enter the verification code and the password you want to set, you can register. Click “Register” button on the page to implement the method of mobile phone number registration. You can also see the registration result in block return.
@IBAction func register(_ sender: Any) { AGCAuth.instance().createUser(withCountryCode: "86", phoneNumber: phoneText.text ?? "", password: phonePassword.text ?? "", verifyCode: phoneVertifyText.text ?? "").onSuccess {(result) in // mobile phone registration success}. OnFailure {(error) in // mobile phone registration failure}}Copy the code
4. After the registration is successful, enter your mobile phone number and password, and click Login. You have completed the steps of registering your mobile phone number using AGConnectAuth.
@IBAction func login(_ sender: Any) { let credential = AGCPhoneAuthProvider.credential(withCountryCode: "86", phoneNumber: phoneText.text ?? "", password: phonePassword.text ?? "") agcauth.instance ().signin (credential: credential).onSuccess {(result) in // login successful}. OnFailure {(error) in // login failed}}Copy the code
5. If you use an unregistered email account, you need to send a verification code to the email address for verification. Enter the email number, click “send verification code” button, the code is executed to request the email verification code method, you can also see the sent result in the return.
@IBAction func emailSendVertifyCode(_ sender: Any) { let setting = AGCVerifyCodeSettings.init(action: AGCVerifyCodeAction.registerLogin, locale: nil, sendInterval: 30) AGCEmailAuthProvider.requestVerifyCode(withEmail: emailText.text ?? "", Settings: setting). OnSuccess {(result) in // email verification code sent successfully}. OnFailure {(error) in // email verification code sent failed}}Copy the code
6. After receiving the verification code, enter the verification code and the password you want to set, and click “Register” to complete registration.
@IBAction func register(_ sender: Any) { AGCAuth.instance().createUser(withEmail: emailText.text ?? "", password: emailPassword.text ?? "", verifyCode: emailVertifyText.text ?? "").onSuccess {(result) in // mailbox registration success}. OnFailure {(error) in // mailbox registration failure}}Copy the code
7. After the registration is successful, enter your email number and password, and click Login. You have completed the steps of registering a mailbox login using AGConnectAuth.
@IBAction func login(_ sender: Any) { let credential = AGCEmailAuthProvider.credential(withEmail: emailText.text ?? "", password: emailPassword.text ?? "") agcauth.instance ().signin (credential: credential).onSuccess {(result) in // login successful}. OnFailure {(error) in // login failed}}Copy the code
8. Call the signOut method to exit the login state.
@IBAction func logout(_ sender: Any) {
AGCAuth.instance().signOut()
}
Copy the code
Packaging test
1. Click the Run button on Xcode to run the app on your phone or emulator. Enter the mobile phone number and click Send Verification code. After receiving the verification code, enter the verification code and the password you want to set. Click Register to check whether the registration is successful. Interruption point View the returned result. Result Displays the user information after the successful login.
2. To verify the mailbox registration and login function, click logout to logout. Otherwise, the login fails again. Enter the email account, click “Send Verification code” button, enter the verification code and the password you want to set, click “Register”, and click “login”. If you view result, you can view the user information after successful login.
conclusion
Authentication service integration is simple, SDK dependence volume is small, can achieve cross-platform sharing, Android and iOS can support, do not need to do different adaptation in different platforms, saving work.
Operation and promotion can be created on the AGC interface, and development and sharing functions can be created with code on the side, which is perfect.
Reference Documents:
Huawei AGC certification service development documentation: developer.huawei.com/consumer/cn… More wonderful content, please see the official BBS huawei developers – developer.huawei.com/consumer/cn…