首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AsWebAuthenticationsession支持通用链接吗?

AsWebAuthenticationsession支持通用链接吗?
EN

Stack Overflow用户
提问于 2020-05-12 09:46:52
回答 2查看 1.3K关注 0票数 13

我使用AsWebAuthenticationsession从另一个应用程序到我的应用程序进行身份验证。我打开AsWebAuthenticationsession,它重定向我的应用程序的通用链接。问题是当它重定向我的应用程序通用链接时,它要求打开App。当它重定向时,我想关闭会话。但是AsWebAuthenticationsession只使用自定义URL方案。如何安全地处理它(因为自定义的URL方案不安全:RFC8252 7.1)

EN

回答 2

Stack Overflow用户

发布于 2022-02-18 22:39:06

我可以确认,在iOS 14或更高版本时,还没有在早期版本上进行测试。

初始化ASWebAuthenticationSession时,可以传入callbackURLScheme: "https"

当身份验证提供者重定向到您的通用链接时,应用程序委托的application(_:continue:restorationHandler:)将使用正确的重定向url触发,但是ASWebAuthenticationSession的完成处理程序不会触发,因此身份验证对话框将保留在屏幕上。

您需要手动保存对ASWebAuthenticationSessioncancel()的引用,从而将其排除在外。

票数 4
EN

Stack Overflow用户

发布于 2022-09-06 02:26:30

您可以尝试使用此方法。

添加单例类以处理此回调

SceneDelegate.swift

代码语言:javascript
复制
@available(iOS 13.0, *)

func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {

    // handle here

    OAuthManager.instance.callBackUserActivity(userActivity: userActivity)

}

OAuthManager.swift

代码语言:javascript
复制
import Foundation

import AuthenticationServices



protocol UserActivityListener {

func callBackUserActivity( userActivity : NSUserActivity )

}



class OAuthManager {

public static let instance = OAuthManager()

var asWebSession: ASWebAuthenticationSession?

}

extension OAuthManager : UserActivityListener {

func callBackUserActivity(userActivity: NSUserActivity) {

    // Get URL components from the incoming user activity.

    guard userActivity.activityType == NSUserActivityTypeBrowsingWeb,

          let incomingURL = userActivity.webpageURL,

          let components = NSURLComponents(url: incomingURL,    resolvingAgainstBaseURL: true) else {

        return

    }

    

    // Check for specific URL components that you need.

    guard let path = components.path,

          let params = components.queryItems else {

        return

    }
    // cancel your ASWebAuthenticationSession
    asWebSession?.cancel()


    print("path = \(userActivity.webpageURL)")



    if let token = params.first(where: { $0.name == "token" })?.value {

        print("token = \(token)")

    }

  }


 }

YourViewController.swift

代码语言:javascript
复制
class YourViewController: UIViewController {
// set your instance
var oauthManager = OAuthManager.instance


private func startSignInAsWebAuthSession() {
    let callbackURLScheme = "YOURAPP"
    guard let authURL = URL(string: "YOUR URL LINK") else { return }
        
        self.oauthManager.asWebSession = ASWebAuthenticationSession.init(url: authURL, callbackURLScheme: callbackURLScheme,completionHandler: { callbackURL, error in
            // we dont listen to the call back, this web authentication session only open for login only
        })
        
        oauthManager.asWebSession?.prefersEphemeralWebBrowserSession = true
        oauthManager.asWebSession?.presentationContextProvider = self
        oauthManager.asWebSession?.start()
}
}

extension YourViewController:    ASWebAuthenticationPresentationContextProviding {
   func presentationAnchor(for session: ASWebAuthenticationSession) ->     ASPresentationAnchor {
       ASPresentationAnchor()
    }
  }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61748589

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档