如何解决使用通用链接作为重定向 URL 通过 Web 服务对用户进行身份验证
我无法使用通用链接处理成功回调。 我使用 Xcode 12.3,iOS 部署目标是 13.0
步骤 1:从类创建 Web 身份验证会话:EMAConnectVC
import UIKit
import AuthenticationServices
class EMAConnectVC: UIViewController,ASWebAuthenticationPresentationContextProviding {
var webAuthSession: aswebauthenticationsession?
override func viewDidLoad() {
super.viewDidLoad()
}
func presentationAnchor(for session: aswebauthenticationsession) -> ASPresentationAnchor {
return view.window!
}
//Step 2
func getAuthTokenWithWebLogin(context: ASWebAuthenticationPresentationContextProviding) {
var authURLStr = ""
authURLStr = "https://id.planday.com/connect/authorize"
authURLStr.append("?client_id=********-****-****-****-************")
authURLStr.append("&response_type=code")
authURLStr.append("&redirect_uri=https://emaplanday.myserver.ch/auth")
authURLStr.append("&scope=openid%20shift:read")
let authURL = URL(string: authURLStr)
let callbackUrlScheme = "https://emaplanday.myserver.ch/auth"
self.webAuthSession = aswebauthenticationsession.init(url: authURL!,callbackURLScheme: callbackUrlScheme,completionHandler: { (callBack:URL?,error:Error?) in
// handle auth response
guard error == nil,let successURL = callBack else {
//Case on Cancel
print(error!)
return
}
//Case on Success
let oauthToken = NSURLComponents(string:(successURL.absoluteString))?.queryItems?.filter({
$0.name == "code"}).first
})
self.webAuthSession?.presentationContextProvider = self
self.webAuthSession?.start()
}
}
第 2 步:启动身份验证流程
用户触摸按钮进行连接并调用 getAuthTokenWithWebLogin 方法。会话加载外部认证网页并要求用户输入用户名和密码。
取消案例 如果用户按下取消按钮,则调用闭包,会话自动关闭相应的浏览器视图。
成功案例 在用户进行身份验证后,身份验证提供程序重定向到一个通用 URL,如下所示 https://emaplanday.myserver.ch/auth (我在我的网络服务器上进行了必要的配置以将此 URL 重定向到我的应用程序)
然后调用 SceneDelegate.swift 类,但从未调用 WebAuthSession 闭包,并且永远不会关闭外部身份验证视图。
import UIKit
import AuthenticationServices
var globalPresentationAnchor: ASPresentationAnchor? = nil
class SceneDelegate: UIResponder,UIWindowSceneDelegate {
var window: UIWindow?
let emaConnectVC = EMAConnectVC()
func scene(_ scene: UIScene,willConnectTo session: UIScenesession,options connectionoptions: UIScene.Connectionoptions) {
guard let _ = (scene as? UIWindowScene) else { return }
globalPresentationAnchor = window
}
func scene(_ scene: UIScene,continue userActivity: NSUserActivity) {
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
self.window = window
window.makeKeyAndVisible()
}
}
func sceneDiddisconnect(_ scene: UIScene) {
}
func sceneDidBecomeActive(_ scene: UIScene) {
}
func sceneWillResignActive(_ scene: UIScene) {
}
func sceneWillEnterForeground(_ scene: UIScene) {
}
func sceneDidEnterBackground(_ scene: UIScene) {
(UIApplication.shared.delegate as? AppDelegate)?.saveContext()
}
}
当调用 SceneDelegate 类中的方法 Scene(_ ... continue userActivity) 时,如何告诉 webAuthSession 闭包?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。