Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to close page in UIWebView and go back to app

Tags:

ios

uiwebview

In my app by pressing a button I want to open UIWebView on full screen, the UIWebView will load a HTML page that will hold a button that will close the UIWebView and return to app.

The problem is that I fail to make the button close the page and return to app. I tried parent.history.back() and history.back and several versions of self.close() but nothing seems to work (BTW it work in browser but not from UIWebView.

any idea? thanks -Z

like image 492
zohar Avatar asked Nov 18 '10 18:11

zohar


People also ask

How do I close webView?

Add a close button and on its click set: webview. setVisibility(View. INVISIBLE); webview.

What is use of UIWebView?

What is UIWebView? UIWebView is a deprecated iOS user interface control in Apple's UIKit framework. It loads HTML files and web content into an app view, rendering them as they would appear in a browser window.

What is iOS UIWebView?

A view that embeds web content in your app.


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
like image 195
Aaron Saunders Avatar answered Oct 02 '22 05:10

Aaron Saunders


Updated for Swift 3:

If you want to close page of UIWebView and go back to app, use below of code:

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)
    }
}
like image 32
Kiran Jadhav Avatar answered Oct 02 '22 05:10

Kiran Jadhav