How to close a page in UIWebView and return to the application

In my application, by pressing a button, I want to open UIWebView in full screen, UIWebView load an HTML page on which a button will be held, which will close UIWebView and return to the application.

The problem is that I cannot force the button to close the page and return to the application. I tried parent.history.back() and history.back and several versions of self.close() but nothing works (BTW works in the browser, but not from UIWebView .

any idea? thanks -Z

+4
source share
2 answers
 [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 
+10
source

Updated for Swift 3:

If you want to close the UIWebView page and return to the application, use the code below:

 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) } } 
0
source

All Articles