英文:
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 = "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
}
}
}
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论