从SwiftUI中的WKWebView获取响应。

huangapple go评论73阅读模式
英文:

Get the response from WKWebView in SwiftUI

问题

以下是您要翻译的内容:

"I am new in networking, and trying to build an app that retrieves users' top Spotify tracks using SwiftUI. Currently I'm stuck trying to implement authorization to your Spotify account. Here is my View with authorization:

import SwiftUI
import WebKit

struct AuthView: UIViewRepresentable {
typealias UIViewType = WKWebView

let webView: WKWebView
var url: String = "https://accounts.spotify.com/en/authorize?client_id=...&redirect_uri=https://pie-test-login/callback&response_type=token"

init() {
    webView = WKWebView(frame: .zero)
    let urlRequest = URLRequest.init(url: URL.init(string: url)!)
    webView.load(urlRequest)
}

func makeUIView(context: Context) -> WKWebView {
    webView
}
func updateUIView(_ uiView: WKWebView, context: Context) {
}

}

struct AuthView_Previews: PreviewProvider {
static var previews: some View {
AuthView()
}
}

It allows the user to log in, but I don't understand how to get the response with the authorization code after the authorization. Will appreciate any help/comments!"

英文:

I am new in networking, and trying to build and app that retrieves users top Spotify tracks using SwiftUI. Currently I'm stuck trying to implement authorization to your Spotify account. Here is my View with authorization:

import SwiftUI
import WebKit

struct AuthView: UIViewRepresentable {
    typealias UIViewType = WKWebView
    
    let webView: WKWebView
    var url: String = "https://accounts.spotify.com/en/authorize?client_id=...&redirect_uri=https://pie-test-login/callback&response_type=token"
    
    init() {
        webView = WKWebView(frame: .zero)
        let urlRequest = URLRequest.init(url: URL.init(string: url)!)
        webView.load(urlRequest)
    }
    
    func makeUIView(context: Context) -> WKWebView {
        webView
    }
    func updateUIView(_ uiView: WKWebView, context: Context) {
    }
}

struct AuthView_Previews: PreviewProvider {
    static var previews: some View {
        AuthView()
    }
}

It allows user to login, but I don't understand how to get the response with the authorization code after the authorization. Will appreciate any help/comments!

答案1

得分: 0

I managed to find an answer:

我找到了一个答案:

struct AuthView: UIViewControllerRepresentable {
    typealias UIViewControllerType = AuthViewController
    
    @Binding var isPresented: Bool
    @Binding var accessToken: String?
    
    var url: String = "https://accounts.spotify.com/en/authorize?client_id=...&redirect_uri=REDIRECT_STRING&response_type=code"
    
    func makeUIViewController(context: Context) -> AuthViewController {
        let viewController = AuthViewController(isPresented: $isPresented, accessToken: $accessToken)
        viewController.url = url
        return viewController
    }
    
    func updateUIViewController(_ uiViewController: AuthViewController, context: Context) {
    }
}

class AuthViewController: UIViewController, WKNavigationDelegate {
    private var webView: WKWebView!
    var url: String = ""
    @Binding var isPresented: Bool
    @Binding var accessToken: String?
    
    init(isPresented: Binding<Bool>, accessToken: Binding<String?>) {
        _isPresented = isPresented
        _accessToken = accessToken
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupWebView()
        loadWebView()
    }
    
    private func setupWebView() {
        let webConfiguration = WKWebViewConfiguration()
        webView = WKWebView(frame: .zero, configuration: webConfiguration)
        webView.navigationDelegate = self
        view = webView
    }
    
    private func loadWebView() {
        guard let url = URL(string: url) else {
            return
        }
        let urlRequest = URLRequest(url: url)
        webView.load(urlRequest)
    }
    
    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
        guard let url = navigationAction.request.url else {
            decisionHandler(.allow)
            return
        }
        
        let urlString = url.absoluteString
        let api = API()
        
        // Check if the URL contains your redirect URI
        if urlString.contains(REDIRECT_STRING) {
            // Extract the authorization code from the URL
        }
    }
}

我们需要创建一个委托,以查找URL中的更新,并当更新并包含重定向URL时执行操作。

英文:

I managed to find an answer:

struct AuthView: UIViewControllerRepresentable {
    typealias UIViewControllerType = AuthViewController
    
    @Binding var isPresented: Bool
    @Binding var accessToken: String?
    
    var url: String = &quot;https://accounts.spotify.com/en/authorize?client_id=...&amp;redirect_uri= REDIRECT_STRING&amp;response_type=code&quot;
    
    func makeUIViewController(context: Context) -&gt; AuthViewController {
        let viewController = AuthViewController(isPresented: $isPresented, accessToken: $accessToken)
        viewController.url = url
        return viewController
    }
    
    func updateUIViewController(_ uiViewController: AuthViewController, context: Context) {
    }
    
}

class AuthViewController: UIViewController, WKNavigationDelegate {
    private var webView: WKWebView!
    var url: String = &quot;&quot;
    @Binding var isPresented: Bool
    @Binding var accessToken: String?
    
    init(isPresented: Binding&lt;Bool&gt;, accessToken: Binding&lt;String?&gt;) {
            _isPresented = isPresented
            _accessToken = accessToken
            super.init(nibName: nil, bundle: nil)
        }
        
    required init?(coder aDecoder: NSCoder) {
        fatalError(&quot;init(coder:) has not been implemented&quot;)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupWebView()
        loadWebView()
    }
    
    private func setupWebView() {
        let webConfiguration = WKWebViewConfiguration()
        webView = WKWebView(frame: .zero, configuration: webConfiguration)
        webView.navigationDelegate = self
        view = webView
    }
    
    private func loadWebView() {
        guard let url = URL(string: url) else {
            return
        }
        let urlRequest = URLRequest(url: url)
        webView.load(urlRequest)
    }
    
    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -&gt; Void) {
        guard let url = navigationAction.request.url else {
            decisionHandler(.allow)
            return
        }
        
        let urlString = url.absoluteString
        let api = API()
        
        // Check if the URL contains your redirect URI
        if urlString.contains(REDIRECT_STRING) {
            // Extract the authorization code from the URL
   
            }
      
    } 
    
}

We need to create delegate, that will look for updates in url, and when it's updated and it contains the redirect url, we do operations

huangapple
  • 本文由 发表于 2023年5月17日 19:17:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76271504.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定