¶ Xiaomi login
¶ Preparation
Configure on Xiaomi Open Platform (opens new window) and Authing Console (opens new window), please refer to Xiaomi Official Document (opens new window).
¶ Integrated Xiaomi login
¶ Step 1: Add Xiaomi dependency
Enter: https://github.com/Authing/authing-binary in the swift package search bar.
Authing-binary (opens new window) depends on Guard-iOS SDK (opens new window).
Select Up to Next Major Version 1.0.0 for the dependency rule.
Check Xiaomi after Add Package.
Xiaomi depends on Guard-iOS 1.4.4 (opens new window) later versions.
¶ Step 2: Modify project configuration
Configure Xiaomi login component bounce URL:
- Select the Xcode project, click the plus sign in Targets -> Info -> URL Types.
- Identifier fill in xiaomi.
- URL Schemes fill in the AppId of Xiaomi Open Platform.
¶ Step 3: Initialize millet login
import Guard
import Xiaomi
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
Authing.start(<#AUTHING_APP_ID#>)
// appId: Xiaomi's AppId
// redirectUrl: login callback address
Xiaomi.register(appId: <#your_xiaomi_appId#>, redirectUrl: <#your_xiaomi_redirectUrl#>)
}
¶ Step 4: Handle Xiaomi login callback
After Xiaomi returns to the app, if SceneDelegate is used, the following functions need to be overloaded in SceneDelegate.swift:
func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
if let url = URLContexts.first?.url{
_ = Xiaomi.handleOpenUrl(url: url)
}
}
If SceneDelegate is not used, it needs to be overloaded in AppDelegate:
func application(_ app: UIApplication, open url: URL, options: [UIApplication. OpenURLOptionsKey : Any] = [:]) -> Bool {
return Xiaomi.handleOpenUrl(url: url)
}
¶ Step 5: Initiate Xiaomi login authorization
¶ Mi authorized login
func login(viewController: UIViewController, _ scopes: [String] = [], completion: @escaping Authing.AuthCompletion) -> Void
parameter
- viewController UIViewController hosting the view
- scopes are empty by default
example
Xiaomi.login(viewController: <#ViewController#>) { code, message, userInfo in
if (code == 200) {
// login successful
// userInfo
}
}
If the developer integrates Xiaomi login by himself, after getting the AuthorizationCode, he can call the following API in exchange for Authing user information:
¶ Log in via Xiaomi authorization code
func loginByXiaomi(_ code: String, completion: @escaping(Int, String?, UserInfo?) -> Void)
parameter
code
Xiaomi authorization code
example
AuthClient().loginByXiaomi(authCode) { code, message, userInfo in
if (code == 200) {
// userInfo: user information
}
}