Ios 如何在UIWebView中关闭页面并返回应用程序

Ios 如何在UIWebView中关闭页面并返回应用程序,ios,uiwebview,Ios,Uiwebview,在我的应用程序中,按下我想要全屏打开UIWebView的按钮,UIWebView将加载一个HTML页面,该页面将包含一个按钮,该按钮将关闭UIWebView并返回应用程序 问题是,我无法使按钮关闭页面并返回应用程序。 我尝试了parent.history.back()和history.back以及几个版本的self.close(),但似乎没有任何效果(顺便说一句,它在浏览器中工作,但在UIWebView中不工作) 有什么想法吗? 谢谢 -Z 为Swift 3更新: 若要关闭UIWebView页面

在我的应用程序中,按下我想要全屏打开
UIWebView
的按钮,
UIWebView
将加载一个HTML页面,该页面将包含一个按钮,该按钮将关闭
UIWebView
并返回应用程序

问题是,我无法使按钮关闭页面并返回应用程序。 我尝试了
parent.history.back()
history.back
以及几个版本的
self.close()
,但似乎没有任何效果(顺便说一句,它在浏览器中工作,但在
UIWebView
中不工作)

有什么想法吗? 谢谢 -Z

为Swift 3更新:

若要关闭UIWebView页面并返回应用程序,请使用以下代码:

import UIKit

class ViewController: UIViewController, UIWebViewDelegate{

    @IBOutlet weak var mWebView: UIWebView!

    override func viewDidLoad() {
        super.viewDidLoad()
        mWebView.delegate = self
    }

    override func viewWillAppear(_ animated: Bool) {
        self.loadWebView()
    }

    func loadWebView()  {
        mWebView.loadRequest(URLRequest(url: URL(string: "https://stackoverflow.com/")!))
    }

    func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
        print("request: \(request.description)")
        if request.description == "https://stackoverflow.com/users/login"{
            //do close window magic here!!
            print("url matches...")
            stopLoading()
            return false
        }
        return true
    }

    func stopLoading() {
        mWebView.removeFromSuperview()
        self.moveToVC()
    }

    func moveToVC()  {
        print("Write code where you want to go in app")
        // Note: [you use push or present here]
        let vc = 
          self.storyboard?.instantiateViewController(withIdentifier: 
          "storyboardID") as! YourViewControllerName
        self.navigationController?.pushViewController(vc, animated: true)
    }
}

谢谢,我想知道如何使用WKWebView实现这一点,因为WKWebView的委托中没有基于请求的委托方法。到目前为止,我在使用webView方法时运气不佳!
import UIKit

class ViewController: UIViewController, UIWebViewDelegate{

    @IBOutlet weak var mWebView: UIWebView!

    override func viewDidLoad() {
        super.viewDidLoad()
        mWebView.delegate = self
    }

    override func viewWillAppear(_ animated: Bool) {
        self.loadWebView()
    }

    func loadWebView()  {
        mWebView.loadRequest(URLRequest(url: URL(string: "https://stackoverflow.com/")!))
    }

    func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
        print("request: \(request.description)")
        if request.description == "https://stackoverflow.com/users/login"{
            //do close window magic here!!
            print("url matches...")
            stopLoading()
            return false
        }
        return true
    }

    func stopLoading() {
        mWebView.removeFromSuperview()
        self.moveToVC()
    }

    func moveToVC()  {
        print("Write code where you want to go in app")
        // Note: [you use push or present here]
        let vc = 
          self.storyboard?.instantiateViewController(withIdentifier: 
          "storyboardID") as! YourViewControllerName
        self.navigationController?.pushViewController(vc, animated: true)
    }
}