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