是否可以从SwiftUI使用ASWebAuthenticationSession? [英] Is it possible to use ASWebAuthenticationSession from SwiftUI?

查看:182
本文介绍了是否可以从SwiftUI使用ASWebAuthenticationSession?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用ASWebAuthenticationSession针对OAuth API进行身份验证,但是它似乎无法从SwiftUI使用. 这就是我想要的:

I would like to authenticate against an OAuth API usingASWebAuthenticationSession however it doesn't seem to be usable from SwiftUI. This is what I would like to have:

struct ContentView: View: ASWebAuthenticationPresentationContextProviding {
    var body: some View {
        NavigationView {
            VStack {
                Button("Hello World", {
                    // Run oauth flow
                }
            }
        }
        .navigationBarTitle(Text("Greed of Savin"))
        .navigationViewStyle(StackNavigationViewStyle())
    }

    func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor {
        return BungieApi.sharedInstance.presentationAnchor ?? ASPresentationAnchor()
    }
}

}

它需要采用与SwiftUI的Views不兼容的协议ASWebAuthenticationPresentationContextProviding.

It requires adopting the protocol ASWebAuthenticationPresentationContextProviding which is not compatible with SwiftUI's Views.

我可以通过重定向到一个ViewController来解决这个问题,该ViewController随后可以提供ASWebAuthenticationPresentationContextProviding,但是它增加了一个额外的视图/导航步骤.

I can get past this by redirecting to a ViewController that can then provides the ASWebAuthenticationPresentationContextProviding, but that adds an additional view/navigation step.

有什么方法可以从SwiftUI中使用ASWebAuthenticationSession而不放入ViewController中吗?

Is there any way to use ASWebAuthenticationSession from SwiftUI without dropping into a ViewController?

推荐答案

我分三部分解决了这个问题:

I solved this in three parts:

首先,在SceneDelegate.swift中的设置过程中,在全局对象中捕获窗口:

First, capture the window in a global object during setup in SceneDelegate.swift:

var globalPresentationAnchor: ASPresentationAnchor? = nil
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        // ...            
        globalPresentationAnchor = window
    }
}

然后,创建一个小的ViewController,以使用ASWebAuthenticationSession将该窗口对象提供给:

Then, create a small ViewController to provide that window object to the using ASWebAuthenticationSession:

class ShimViewController: UIViewController, ASWebAuthenticationPresentationContextProviding
{
    func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor {
        // Perhaps I don't need the window object at all, and can just use:
        // return ASPresentationAnchor()
        return globalPresentationAnchor ?? ASPresentationAnchor()
    }
}

最后,调用身份验证API,将ShimViewController提供为演示者.

Finally, call the authentication API, providing the ShimViewController as the presenter.

    let session = ASWebAuthenticationSession(/**/)
    session.presentationContextProvider = ShimViewController()
    session.start()

这篇关于是否可以从SwiftUI使用ASWebAuthenticationSession?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆