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
Add a close button and on its click set: webview. setVisibility(View. INVISIBLE); webview.
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.
A view that embeds web content in your app.
[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
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)
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With