簡體   English   中英

如何在UIWebView中關閉頁面並返回應用程序

[英]How to close page in UIWebView and go back to app

在我的應用程序中,通過按下我想在全屏幕上打開UIWebView的按鈕, UIWebView將加載一個HTML頁面,該頁面將保存一個按鈕,該按鈕將關閉UIWebView並返回到應用程序。

問題是我無法使按鈕關閉頁面並返回應用程序。 我試圖parent.history.back()history.back和幾個版本self.close()但似乎沒有任何工作(BTW它在瀏覽器中運行,但不會從UIWebView

任何想法? 謝謝-Z

[UIWebViewDelegate][1] has your answer

    - (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request            
                                    navigationType:(UIWebViewNavigationType)navigationType {
        if (request.URL == "SOME URL TO CLOSE WINDOW") {
            //do close window magic here!!
            [self stopLoading];
            return NO;
        }
        return YES;
    }
-(void)stopLoading{
    [_webView removeFromSuperview];
}

  [1]: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIWebViewDelegate_Protocol/Reference/Reference.html

針對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)
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM