gpt4 book ai didi

ios - 检索 WebVie URL Swift ||无法将类型 'ViewController' 的值分配给类型 'UIWebViewDelegate?'

转载 作者:行者123 更新时间:2023-11-30 12:13:09 28 4
gpt4 key购买 nike

我想加载一个网页,然后能够在用户登录并重定向后检索当前网页。我已经实现了网页的加载,但在用户移动到新页面后无法检索 URL。我当前的代码是:

@IBOutlet weak var webView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
needlogin()
}

func needlogin(){
let url = URL(string: "https://www.instagram.com/oauth/authorize/?client_id="+clientid+"&redirect_uri="+redirecturl+"&response_type=token&scope=likes+comments+public_content+follower_list+relationships")
let urlrequest = URLRequest(url: url!)
webView.loadRequest(urlrequest)
self.webView.delegate = self

}

func webViewDidFinishLoad(_ webView: UIWebView){

}

我遇到的问题是 self.webView.delegate = self 。我收到错误消息:

Cannot assign value of type 'ViewController' to type 'UIWebViewDelegate?'

此外,我根本不知道如何检索 URL,非常感谢您的帮助。

最佳答案

您需要遵守 UIWebviewDelegate 协议(protocol)。所以你需要做类似的事情

class ViewController: UIViewController, UIWebViewDelegate {

}

位于类定义的顶部。

要在 WebView 加载完成后检索 URL,在 webViewDidFinishLoad 方法中,您需要执行类似 webView.request.URL 的操作。

免责声明:对于 iOS + 应用程序,您应该使用 WKWebview 而不是 UIWebview。对于您的示例,执行此操作的方法如下:

import UIKit
import WebKit

class WebviewController: UIViewController, WKNavigationDelegate {

var wkWebview : WKWebView?

override func viewDidLoad() {
super.viewDidLoad()

needsLogin()

}

func needsLogin() {

wkWebview = WKWebView.init(frame: self.view.bounds)
wkWebview?.navigationDelegate = self
self.view.addSubview(wkWebview!)

let url = URL(string: "https://www.instagram.com/oauth/authorize/? client_id="+clientid+"&redirect_uri="+redirecturl+"&response_type=token&scope=likes+comments+public_content+follower_list+relationships")
let urlRequest: URLRequest = URLRequest.init(url: url!)
wkWebview?.load(urlRequest)

}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

public func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
print("\(error.localizedDescription)")
}

public func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
// This is the URL you need.
let url = webView.url
}
}

关于ios - 检索 WebVie URL Swift ||无法将类型 'ViewController' 的值分配给类型 'UIWebViewDelegate?',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45784234/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com