关于Cidaas的更多信息
想了解更多关于 Cidaas 的信息,请访问 CIDaaS
Cidaas 文档
需求
Operating System : iOS 10.0 or above
Xcode : 9
Swift : 4.0
安装
Cidaas-SDK 通过 CocoaPods 提供。要安装它,只需将以下行添加到Podfile中:
pod 'Cidaas-SDK', '~> 10.0'
开始使用
以下步骤用于使用此 Cidaas-SDK 登录控件
- 创建一个plist文件,并将所有输入填入键值对中。以下是所需输入。
plist文件应该如下所示
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key> AuthorizationURL </key>
<string> Your authorization url </string>
<key> TokenURL </key>
<string> Your token url </string>
<key> UserInfoURL </key>
<string> Your user info url </string>
<key> SocialUrl </key>
<string> your social url </string>
<key> RedirectURI </key>
<string> Your redirect uri </string>
<key> LogoutURL </key>
<string> Your logout url </string>
<key> ClientID </key>
<string> Your client id </string>
<key> ViewType </key>
<string> Your view type </string>
</dict>
</plist>
- 在AppDelegate中提及文件名
CidaasSDK.plistFilename = "Your file name"
获取应用ID和URL
您可以从cidaas AdminUI获取应用程序的属性文件
Cidaas-SDK的集成有2种方式
-
原生浏览器集成 - 打开Safari浏览器进行登录。不支持原生Facebook和Google SDK
-
WKWebview集成 - 打开webview进行登录。此外还支持原生Facebook和Google SDK
原生浏览器集成
- 创建CidaasSDK实例
var cidaas = CidaasSDK()
(or)
var cidaas = CidaasSDK.shared
- 将safariDelegate分配给当前视图控制器
cidaas.safariDelegate = self
- 调用loginWithNativeBrowser()方法并获取访问令牌和用户信息作为回调
cidaas.loginWithNativeBrowser { (token_response) in
// your code
}
- 调用showUserProfileInBrowser()函数以显示用户配置文件
cidaas.showUserProfileInBrowser(accessToken : "your access token")
or
cidaas.showUserProfileInBrowser(userId : "your user id")
通过调用添加您的用户配置文件URL是可选的
CidaasSDK.user_profile_url = "https://your user profile url"
使用customScheme或universalLinks作为必填的redirectURL
- 如果使用自定义方案,配置URL类型并在AppDelegate的open url方法中恢复SDK
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
CidaasSDK.shared.resume(open: url)
return true
}
- 如果使用通用链接,配置域名设置并在AppDelegate的userActivity方法中恢复SDK
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([Any]?) -> Void) -> Bool {
let url = userActivity.webpageURL!
CidaasSDK.shared.resume(open: url)
return true
}
WKWebview集成
-
在Storyboard中拖放一个空视图
-
在属性窗口中将类的名称更改为CidaasSDK
-
为该类创建一个 IBOutlet并将其视为一个对象
@IBOutlet weak var cidaas : CidaasSDK!
- 继承WKNavigationDelegate并调用方法
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
cidaas.webView(webView, didStartProvisionalNavigation: navigation)
}
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
cidaas.webView(webView, decidePolicyFor: navigationAction, decisionHandler: decisionHandler)
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
cidaas.webView(webView, didFinish: navigation)
}
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
cidaas.webView(webView, didFail: navigation, withError: error)
}
- 通过设置当前视图控制器作为委托来分配委托
cidaas.delegate = self
- 调用 login() 函数,并通过回调获取访问令牌和用户信息
cidaas.login { token_response in
// your code here
}
- 调用 showUserProfile() 函数以显示用户资料
cidaas.showUserProfile(accessToken : "your access token")
or
cidaas.showUserProfile(userId : "your user id")
通过调用添加您的用户配置文件URL是可选的
CidaasSDK.user_profile_url = "https://your user profile url"
常用函数
- 通过传递 access_token 参数调用 getUserInfo() 函数以获取用户信息
CidaasSDK.getUserInfo(accessToken: "your access token") { user_info_response in
// your code here
}
- 调用 getAccessToken() 函数,通过传递 user_id 参数来验证访问令牌
CidaasSDK.getAccessToken(userId: "your userId") { token_response in
// your code here
}
- 调用 logout() 函数退出会话
CidaasSDK.logout(userId: "your userId") { logout_response in
// your code here
}
- 如果要与原生 Facebook 和 Google SDK 集成,请在 AppDelegate.swift 中添加以下行
CidaasSDK.enableNativeFacebook = true
CidaasSDK.enableNativeGoogle = true
- 通过传递 refresh_token 和 user_id 参数调用 getAccessTokenSync() 函数以获取访问令牌(仅用于同步目的)
CidaasSDK.getAccessTokenSync(refreshToken: (callbackEntity.accessTokenEntity?.refreshToken)!, userId: (callbackEntity.userInfoEntity?.ssoId)!, callback: { (resp) in
// your code here
})
在 ViewController.swift 中创建 CidaasFacebook 和 CidaasGoogle 的实例,并将委托分配给当前视图控制器
有关更多详细信息,请参阅 Cidaas-Facebook 和 Cidaas-Google
Note : Native Facebook and Native Google SDK supportability is only for WKWebview integration
添加自定义加载器的步骤
-
在你的 ViewController.swift 中实现 LoaderDelegate 协议
-
实现 showLoader() 和 hideLoader() 方法
-
在方法内部编写添加自定义加载器的代码
-
将加载器委托分配给当前视图控制器
CidaasSDK.loaderDelegate = self
示例代码
ViewController.swift
import UIKit
class ViewController: UIViewController {
var cidaas_sdk : CidaasSDK = CidaasSDK.shared
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func openBrowser(_ sender: Any) {
let vc = self.storyboard?.instantiateViewController(withIdentifier: "NativeBrowserViewController") as! NativeBrowserViewController
self.navigationController?.pushViewController(vc, animated: true)
}
@IBAction func openWebView(_ sender: Any) {
let vc = self.storyboard?.instantiateViewController(withIdentifier: "WKViewController") as! WKViewController
self.navigationController?.pushViewController(vc, animated: true)
}
}
NativeBrowserViewController.swift
import UIKit
import Cidaas_SDK
class NativeBrowserViewController: UIViewController, LoaderDelegate {
var cidaas_sdk : CidaasSDK = CidaasSDK.shared
var visible : Bool = false
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(_ animated: Bool) {
if visible == false {
visible = true
CidaasSDK.loaderDelegate = self
loginWithNativeBrowser()
}
}
func loginWithNativeBrowser() {
cidaas_sdk.safariDelegate = self
cidaas_sdk.loginWithNativeBrowser { (callbackEntity) in
let alert = UIAlertController(title: "Access Token", message: "\(callbackEntity.accessTokenEntity?.accessToken ?? "")", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Done", style: .default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func showLoader() {
CustomLoader.sharedCustomLoaderInstance.showLoader(self.view, using: nil) { (hud) in
}
}
func hideLoader() {
CustomLoader.sharedCustomLoaderInstance.hideLoader(self.view)
}
@IBAction func backPressed(_ sender: Any) {
self.navigationController?.popViewController(animated: true)
}
}
WKViewController.swift
import UIKit
import WebKit
import Cidaas_SDK
class WKViewController: UIViewController, WKNavigationDelegate, LoaderDelegate {
@IBOutlet var cidaas_sdk: CidaasSDK!
override func viewDidLoad() {
super.viewDidLoad()
CidaasSDK.loaderDelegate = self
login()
}
func login() {
cidaas_sdk.delegate = self
cidaas_sdk.login { (callbackEntity) in
let alert = UIAlertController(title: "Access Token", message: "\(callbackEntity.accessTokenEntity?.accessToken ?? "")", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Done", style: .default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func showLoader() {
CustomLoader.sharedCustomLoaderInstance.showLoader(self.view, using: nil) { (hud) in
}
}
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
cidaas_sdk.webView(webView, didStartProvisionalNavigation: navigation)
}
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
cidaas_sdk.webView(webView, decidePolicyFor: navigationAction, decisionHandler: decisionHandler)
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
cidaas_sdk.webView(webView, didFinish: navigation)
}
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
cidaas_sdk.webView(webView, didFail: navigation, withError: error)
}
func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
cidaas_sdk.webView(webView, didFail: navigation, withError: error)
}
func hideLoader() {
CustomLoader.sharedCustomLoaderInstance.hideLoader(self.view)
}
@IBAction func backPressed(_ sender: Any) {
self.navigationController?.popViewController(animated: true)
}
}
AppDelegate.swift
import UIKit
import Cidaas_SDK
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
CidaasSDK.plistFilename = "CidaasURLProperty"
return true
}
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([Any]?) -> Void) -> Bool {
let url = userActivity.webpageURL!
CidaasSDK.shared.resume(open: url)
return true
}
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
CidaasSDK.shared.resume(open: url)
return true
}
func applicationWillResignActive(_ application: UIApplication) {
}
func applicationDidEnterBackground(_ application: UIApplication) {
}
func applicationWillEnterForeground(_ application: UIApplication) {
}
func applicationDidBecomeActive(_ application: UIApplication) {
}
func applicationWillTerminate(_ application: UIApplication) {
}
}
UserProfileViewController.swift
import UIKit
import Cidaas_SDK
import WebKit
class UserProfileViewController: UIViewController, LoaderDelegate, WKNavigationDelegate {
@IBOutlet var cidaas_sdk: CidaasSDK!
var user_id : String!
var access_token : String!
override func viewDidLoad() {
super.viewDidLoad()
CidaasSDK.loaderDelegate = self
cidaas_sdk.delegate = self
self.navigationItem.setHidesBackButton(true, animated: true)
cidaas_sdk.showUserProfile(accessToken: access_token)
}
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
cidaas_sdk.webView(webView, decidePolicyFor: navigationAction, decisionHandler: decisionHandler)
}
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
cidaas_sdk.webView(webView, didStartProvisionalNavigation: navigation)
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
cidaas_sdk.webView(webView, didFinish: navigation)
}
func showLoader() {
CustomLoader.sharedCustomLoaderInstance.showLoader(self.view, using: nil) { (hud) in
}
}
func hideLoader() {
CustomLoader.sharedCustomLoaderInstance.hideLoader(self.view)
}
@IBAction func backButtonAction(_ sender: Any) {
self.navigationController?.popViewController(animated: true)
}
}
屏幕截图
注解
- 如果您遗漏了任何一个属性或代理,都不会显示任何内容
帮助和支持
了解更多支持,请访问 CIDaaS 支持