iOS SDK
GG SDK là bộ thư viện để các ứng dụng có thể tương tác với GG Platform. GG SDK bao gồm các chức năng chính như sau:
- Đăng nhập bằng tài khoản GG, Facebook, PlayNow.
- Hỗ trợ ứng dụng lấy thông tin profile của người dùng.
- Hiện tại GG SDK hỗ trợ tất cả các thiệt bị cài đặt hệ điều hành iOS 11.0 trở lên.
Tham khảo Demo
Tích hợp
1. Thêm GG SDK vào dự án
Swift Package Manager
dependencies: [
.package(url: "https://bitbucket.org/vngecn/ecn-ios-sdk.git", from: "0.5.0"),
]
Sau đó, từ Xcode, chọn Target mà bạn muốn sử dụng SDK, mở tab General
-> Framworks, Libraries and Embedded Content
-> chọn GGSignIn
.
Trong trường hợp muốn tích hợp Facebook bằng GG SDK, bạn có thể chọn thêm GGSignInWithFacebook
.
CocoaPods
source 'https://github.com/CocoaPods/Specs.git'
source 'git@bitbucket.org:vngecn/cocoapods-specs.git'
pod 'GGSignIn', '~> 0.6.0'
pod 'GGSignIn/FacebookIntegration', '~> 0.6.0'
2. Cấu hình dự án
- Thêm đoạn mã sau vào file Info.plist của ứng dụng:
Để đăng ký ứng dụng, bạn vui lòng liên hệ qua email BaoNQ3 để lấy thông tin clientId
và clientToken
.
Sau đó làm theo các buớc dưới đây để tích hợp GG SDK vào dự án của bạn:
// Client Id
<key>GGVNGClientID</key>
<string>(client_id)</string>
// Client Token
<key>GGVNGClientToken</key>
<string>(client_token)</string>
// Url Scheme
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>gg-(client_id)</string>
</array>
</dict>
</array>
// App query scheme
<key>LSApplicationQueriesSchemes</key>
<array>
<string>ggvng-api</string>
</array>
- Thay thế `client_id`, `client_token` bằng `Client ID` và `Client Token` mà GG cấp của ứng dụng của bạn.
Để tích hợp Facebook SDK, vui lòng đăng ký và cấu hình ứng dụng của bạn theo hướng dẫn tại Facebook Login for iOS - Quickstart
3. Thiết lập khởi tạo
Vui lòng thêm mã khởi tạo sau vào AppDelegate.swift
import UIKit
import GGSignIn
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_: UIApplication, didFinishLaunchingWithOptions _: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Thêm dòng này trong trường hợp muốn tích hợp Facebook SDK thông qua GGSDK.
GGSignIn.registerFBSDK(application, didFinishLaunchingWithOptions: launchOptions)
return true
}
func application(
_ app: UIApplication,
open url: URL,
options: [UIApplication.OpenURLOptionsKey : Any] = [:]
) -> Bool {
GGSignIn.shared.application(
app,
open: url,
sourceApplication: options[UIApplication.OpenURLOptionsKey.sourceApplication] as? String,
annotation: options[UIApplication.OpenURLOptionsKey.annotation]
)
// Thêm dòng này trong trường hợp muốn tích hợp Facebook SDK thông qua GGSDK.
GGSignIn.handleFBSDKURL(app, open: url)
}
}
Đăng nhập
1. Đăng nhập bằng GG
Thêm phương thức đăng nhập bằng GG SDK vào mã của bạn
let config = AuthConfiguration()
// Tuỳ chỉnh phương thức đăng nhập nếu cần thiết
config.preferredMethod = .google
// Tuỳ chỉnh cách hiển thị màn hình đăng nhập nếu cần thiết
// Tham khảo mục [AuthMode] bên dưới
config.authMode = .selectAccount
GGSignIn.shared.authenticate(
configuration: config,
scopes: [
"profile.userid",
"profile.displayname",
"profile.avatar"
], viewController: self) { credential, error in
if credential != nil {
// Đăng nhập thành công
} else {
// Xử lý lỗi
}
}
Sau khi đăng nhập thành công, bạn có thể lấy thông tin đăng nhập bằng cách sau:
let credential = GGSignIn.shared.currentAuthCredential()
AuthMode
Tham khảo AuthMode chi tiết ở AuthMode
2. Đăng nhập vào GG thông qua Third-Party SDK
Hiện tại GG cung cấp các phươc thức để người dùng đăng nhập vào GG thông qua AppleID
, Facebook
và ZingID
.
Bạn có thể cung cấp trực tiếp thông tin đăng nhập cho GGSignIn
thông qua các phương thức AppleIDCredentialProvider
, FacebookCredentialProvider
hoặc ZingIDCredentialProvider
.
3. Chơi ngay
GGSignIn.shared.authenticateWithoutSignIn(scopes: scopes, viewController: self) { credential, error in
}
4. Trường thông tin người dùng
Tham khảo tài liệu thông tin người dùng (scopes)
5. Quick Login / Create Guest
createGuest(configuration: config, viewController: self)
Parameter | Type | Description |
---|---|---|
configuration | AuthConfiguration | Required. The configuration of auth |
let config = AuthConfiguration()
config.scopes = authScopes()
GGSignIn.shared.createGuest(configuration: config, viewController: self){ credential, error in
if credential != nil {
// Đăng nhập thành công
} else {
// Xử lý lỗi
}
}
6. Quick Login / Bind Guest
bindGuest(
configuration: AuthConfiguration = AuthConfiguration(),
preferredMethod: AuthMethod? = nil,
socialAuthProvider: SocialAuthProviding?,
viewController: UIViewController?,
completion: @escaping (_ credential: GGIDAuthCredential?, _ error: Error?) -> Void) {
}
Parameter | Type | Description |
---|---|---|
configuration | AuthConfiguration | Required. The Auth configuration |
socialAuthProvider | SocialAuthProviding | Optional. The social auth providing when use: Apple,Facebook,Google,ZingId |
preferredMethod | AuthMethod | Required. The auth method that user want to bind account |
Example for socials login
let authProvider = ZingIDCredentialProvider(
accountName: info.accountName, hashedPassword: hashedPwd
)
let config = AuthConfiguration()
config.scopes = self.authScopes()
GGSDKConfiguration.preferredLanguageCode = GGSDKLanguageCode(rawValue: "en")
GGSignIn.shared.bindGuest(
configuration: config,
preferredMethod: AuthMethod.zingID,
socialAuthProvider: authProvider,
viewController: self) { [weak self] credential,error in
}
7. Đăng ký Zing
let config = AuthConfiguration()
GGSignIn.shared.authenticate(configuration: config, preferredMethod: .zingIDSignUp, viewController: self) {[weak self] _, error in
}
Đăng xuất
Khi người dùng đăng xuất khỏi ứng dụng, bạn có thể gọi hàm sau để xoá thông tin người dùng được lưu trong cache của GGSDK.
GGSignIn.shared.logout()
Cấu hình cho GG SDK
1. Cài đặt ngôn ngữ
GGSDKConfiguration.preferredLanguageCode = GGSDKLanguageCode(rawValue: "vi")
Tham khảo ngôn ngữ hỗ trợ
2. Tuỳ chọn kênh đăng nhập
Để cấu hình các kênh đăng nhập được hỗ trợ trong ứng dụng, hãy gán vào thuộc tính supportedSignInMethods
của GGSignIn.shared
một mảng các phương thức cần dùng:
GGSignIn.shared.supportedSignInMethods = [.appleID, .google, .facebook]
Các tùy chọn hỗ trợ:
.appleID
.google
.facebook
.zingID
.phoneNumber
.email
⚠️ Lưu ý:
Nếu bạn bật.zingIDSignUp
, thì bắt buộc cấu hình.zingID
trongsupportedSignInMethods
.
Ví dụ cấu hình đầy đủ:
GGSignIn.shared.supportedSignInMethods = [
.appleID,
.google,
.facebook,
.zingID,
.phoneNumber,
.email
]
Open API
1. Lấy thông tin người dùng
GGSignIn.shared.loadCurrentProfile { profile, error in
}
Ngoài ra bạn có thể sử dụng Open API để lấy thông tin dựa vào access token bất kỳ
GGOpenAPI.requestProfile(accessToken: accessToken, callbackQueue: .main) { profile, error in
}
2. Kiểm tra thông tin access token
GGOpenAPI.validateAccessToken(token, callbackQueue: .main) { result, error in
guard let result = result else {
// Có lỗi xảy ra
return
}
if result.isActive {
// Token hợp lệ
} else if result.isExpired {
// Token hết hạn
} else {
// Token không hợp lệ
}
}
3. Lấy lại access token mới bằng refresh token
Trong trường hợp access token hết hạn. Bạn có thể lấy lại token mới bằng refresh token.
GGOpenAPI.refreshAccessToken(refreshToken: token, callbackQueue: .main) { credential, error in
if credential != nil {
// Lấy lại token thành công
} else {
// Có lỗi xảy ra
}
}
4. Thu hồi access token của người dùng
GGOpenAPI.revokeAccessToken(token, callbackQueue: .main) { error in
}
5. Xoá tài khoản người dùng
GGOpenAPI.deleteUserAccount(accessToken: token, callbackQueue: .main) { error in
}
Môi trường Sandbox
Bạn có thể chuyển sang môi trường sandbox bằng câu lệnh sau:
GGSignIn.shared.enableSandbox(true)
Thông tin về GG
Bạn có thể mở trang thông tin về GG bằng câu lệnh sau:
GGSignIn.shared.showAboutPage(self, completion: nil)
Mã lỗi
Tham khảo mã lỗi chi tiết ở Mã lỗi đăng nhập
Mã lỗi từ GG SDK trên iOS
Mã lỗi | Mô tả |
---|---|
user_cancelled | Người dùng huỷ quá trình xác thực |
Ví dụ:
GGSignIn.shared.authenticate(configuration: config, preferredMethod: .google, viewController: self) {[weak self] _, error in
if let error {
self?.showError(error)
} else {
// ...
}
}
private func showError(_ error: Error) {
var title = "Error"
var desc = error.localizedDescription
if let err = error as? GGSDKError {
title = err.ggErrorCode
if(err.ggErrorCode == GGSDKError.ErrorCode.userCancelled) {
desc = "Bạn đã bỏ qua đăng nhập"
}
//
// ...
// Handle các error type khác
//
}
let alert = UIAlertController(title: title, message: desc, preferredStyle: .alert)
alert.addAction(.init(title: "OK", style: .default))
present(alert, animated: true)
}
Nhật ký thay đổi phiên bản
Theo dõi thay đổi của từng phiên bản tại đây.
Hỗ trợ
Nếu có thắc mắc trong quá trình tích hợp, bạn có thể liên hệ trực tiếp qua email BaoNQ3 để được trợ giúp.