在我的应用程序中,我想在全屏幕上打开UIWebView
按钮,UIWebView
将加载一个HTML页面,该页面将保留一个关闭UIWebView
的按钮并返回到app。
问题是我无法关闭页面并返回应用程序。
我尝试了parent.history.back()
和history.back
以及self.close()
的几个版本,但似乎没有任何效果(BTW可以在浏览器中使用,但不能从UIWebView
开始。
任何想法? 谢谢 -Z
答案 0 :(得分:10)
[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
答案 1 :(得分:1)
针对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)
}
}