[英]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.