[英]iOS and Android webview: how to close webview and go back to App?
[英]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.