简体   繁体   English

检索WebVie URL Swift || 无法将类型“ ViewController”的值分配给类型“ UIWebViewDelegate?”

[英]Retrieve WebVie URL Swift || Cannot assign value of type 'ViewController' to type 'UIWebViewDelegate?'

I would like to load a web page then be able to retrieve the current web page after the user has logged in and been redirected. 我想加载一个网页,然后在用户登录并重定向后能够检索当前网页。 I have achieved the loading of a webpage but am unable to retrieve the URL after the user has moved to a new page. 我已经实现了网页的加载,但是在用户移至新页面后无法检索URL。 The current code that I have is: 我拥有的当前代码是:

@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){

}

The problem I have is with self.webView.delegate = self . self.webView.delegate = self的问题是self.webView.delegate = self I get the error message : 我收到错误消息:

Cannot assign value of type 'ViewController' to type 'UIWebViewDelegate?' 无法将类型“ ViewController”的值分配给类型“ UIWebViewDelegate?”

Furthermore I have no clue how to retrieve the URL at all and would appreciate your help. 此外,我根本不知道如何检索URL,不胜感激。

You would need to conform to UIWebviewDelegate protocol. 您将需要遵守UIWebviewDelegate协议。 So you would need to do something like 所以你需要做类似的事情

class ViewController: UIViewController, UIWebViewDelegate {

}

at the top of your class definition. 在类定义的顶部。

To retrieve the URL after the webview has finished loading, within your webViewDidFinishLoad method, you would need to do something like webView.request.URL. 要在完成webview的加载后检索URL,请在webViewDidFinishLoad方法中执行诸如webView.request.URL之类的操作。

Disclaimer: You should be using WKWebview rather than UIWebview for iOS + applications. 免责声明:对于iOS +应用程序,您应该使用WKWebview而不是UIWebview。 The way to do it would be something like this for your example: 对于您的示例,执行此操作的方式应如下所示:

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
   }
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将“ViewController”类型的值分配给“AddContactDelegate”类型? - Cannot assign value of type 'ViewController' to type 'AddContactDelegate?' Swift不能指定类型'()'的值来键入'String?' - Swift cannot assign value of type '()' to type 'String?' 无法将 ViewController 类型的值分配给 UITextFieldDelegate 类型的值? - Cannot assign a value of type ViewController to a value of type UITextFieldDelegate? iOS开发-无法将类型“ ViewController”的值分配给类型“ UITableViewController” - iOS Develop - Cannot assign value of type 'ViewController' to type 'UITableViewController' 无法将类型“ [ViewController.Variable_Name]”的值分配为类型“ [String]” - Cannot assign value of type '[ViewController.Variable_Name]' to type '[String]' Swift:无法指定'Item'类型的值?输入'Item?.Type' - Swift: Cannot assign value of type 'Item?' to type 'Item?.Type' 无法将“Int”类型的值分配给“Int?.Type”类型 Swift 5 - Cannot assign value of type 'Int' to type 'Int?.Type' Swift 5 SWIFT:无法分配类型…/ SwiftyJSON的值 - SWIFT : Cannot assign value of type … / SwiftyJSON Swift无法分配'UINavigationController'类型的值 - Swift cannot assign value of type 'UINavigationController' 无法在Swift中将viewController对象转换为viewController类型 - Cannot convert a viewController object to viewController type in Swift
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM