简体   繁体   English

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

[英]UIWebview : How to notify UIViewController if any of the already loaded link in the web view fails to open

I have a UIWebView controller in which I am loading one Web Application . 我有一个UIWebView控制器,其中正在加载一个Web Application If any of the link inside the web app fails to open, I just need to notify the UIViewController that the particular link fails to open and accordingly I need to show the alert box. 如果Web应用程序内的任何链接都无法打开,我只需要通知UIViewController特定链接无法打开,因此我需要显示警报框。 Can you guys please guide me in this ?? 你们能在这方面指导我吗? Any help will be appreciated. 任何帮助将不胜感激。 Thanks in advance. 提前致谢。

您可以使用以下UIWebView委托方法

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error

In your view controller write this 在你的视图控制器中写这个

In viewdidload 在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;
}

In webviewcontroller 在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];
}

Usually Apple have delegates implemented for all of that: UIWebViewDelegate 通常,Apple为此实现了所有委托: UIWebViewDelegate

You can use: 您可以使用:

webView:didFailLoadWithError: Sent if a web view failed to load a frame. webView:didFailLoadWithError:如果Web视图无法加载框架,则发送。

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error

Parameters 参数

  • webView : The web view that failed to load a frame. webView :无法加载框架的Web视图。
  • error : The error that occurred during loading. error :加载期间发生的错误。

Availability : Available in iOS 2.0 and later. 可用性 :在iOS 2.0和更高版本中可用。

Remember, you should: 请记住,您应该:

yourWebView.delegate = self

and

.h 。H

@interface ViewController : UIViewController <UIWebViewDelegate>

or .m 或.m

@interface ViewController () <UIWebViewDelegate>

Called when UIWebview failed to load request UIWebview无法加载请求时调用

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error

you can get link that failed from UIWebview as follows: 您可以从UIWebview获取失败的链接,如下所示:

In this method, use this code to get url that failed. 在这种方法中,使用此代码获取失败的URL。

NSString *strFailedUrl = UIWebviewObj.request.url.absoluteString;

notify your UiviewController accordingly or show alert 相应地通知您的UiviewController或显示警报

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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