繁体   English   中英

UIWebview:如果Web视图中任何已加载的链接无法打开,如何通知UIViewController

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

参数

  • webView :无法加载框架的Web视图。
  • 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM