►如何在 request 前判別連結的 url 來做前置處理
if (navigationType == UIWebViewNavigationTypeLinkClicked ) {
//表示按下網頁中檔案的連結點
}
1. 宣告
SWIFT : extend UIWebViewDelegate()
Objective-C : @interface ViewController2 : UIViewController <UIWebViewDelegate>
Objective-C : @interface ViewController2 : UIViewController <UIWebViewDelegate>
2. 拉出 webview 參考 @IBOutlet var mWebView: UIWebView!
3. 給定 delegate
3. 給定 delegate
override func viewDidLoad() { mWebView.delegate = self; }
4. 實作 UIWebViewDelegate
SWIFT:
//webView request delegate public func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool { //let urlScheme = request.url!.scheme let urlScheme = request.url!.absoluteString; print("scheme:\(urlScheme)"); if urlScheme.contains("http://www.test.com") { return false;//webview 不再處理 URL } return true; }
Objective-C
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { }
if (navigationType == UIWebViewNavigationTypeLinkClicked ) {
//表示按下網頁中檔案的連結點
}