[英]UIWebview : How to notify UIViewController if any of the already loaded link in the web view fails to open
我有一個UIWebView
控制器,其中正在加載一個Web Application
。 如果Web應用程序內的任何鏈接都無法打開,我只需要通知UIViewController
特定鏈接無法打開,因此我需要顯示警報框。 你們能在這方面指導我嗎? 任何幫助將不勝感激。 提前致謝。
您可以使用以下UIWebView
委托方法
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
在你的視圖控制器中寫這個
在viewdidload
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(receivefailedNotification:)
name:@"failedNotification"
object:nil];
- (void) receivefailedNotification:(NSNotification *) notification // this will trig automatically once we provide info to it in webviewcontroller
{
NSDictionary *userInfo = notification.userInfo;
}
在webviewcontroller中
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
NSLog(@"erroe");
NSDictionary *userInfo = [NSDictionary dictionaryWithObject: erroe forKey:@"OauthoKey"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"failedNotification"
object:self userInfo:userInfo];
}
通常,Apple為此實現了所有委托: UIWebViewDelegate
您可以使用:
webView:didFailLoadWithError:
如果Web視圖無法加載框架,則發送。
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
參數
可用性 :在iOS 2.0和更高版本中可用。
請記住,您應該:
yourWebView.delegate = self
和
。H
@interface ViewController : UIViewController <UIWebViewDelegate>
或.m
@interface ViewController () <UIWebViewDelegate>
當UIWebview
無法加載請求時調用
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
您可以從UIWebview
獲取失敗的鏈接,如下所示:
在這種方法中,使用此代碼獲取失敗的URL。
NSString *strFailedUrl = UIWebviewObj.request.url.absoluteString;
相應地通知您的UiviewController
或顯示警報
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.