簡體   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