【问题标题】:categorise UIWebView errors and display on iphone app?对 UIWebView 错误进行分类并在 iphone 应用上显示?
【发布时间】:2012-07-26 08:44:14
【问题描述】:

UIWebView 的任何类别是否可能出现错误?在我的应用程序中,我需要显示加载 url 时发生的实际错误。我可以在 didFailLoadWithError 方法中打印错误,但会给出关于错误的详细描述,例如

didFailLoadWithError Error Domain=NSURLErrorDomain Code=-1003 "A server with the specified hostname could not be found." UserInfo=0x895fb10 {NSErrorFailingURLStringKey=https://mobilelogin.bwanet.ca/mdninput.html?csphostid=J13y8E9t000Mb3ZK00001H7R, NSErrorFailingURLKey=https://mobilelogin.bwanet.ca/mdninput.html?csphostid=J13y8E9t000Mb3ZK00001H7R, NSLocalizedDescription=A server with the specified hostname could not be found., NSUnderlyingError=0x8c68f20 "A server with the specified hostname could not be found."}

我将如何对这些可能的错误进行分类,我想将其显示为 “找不到指定的主机名” “网址超时错误” 等等

【问题讨论】:

    标签: iphone uiwebview


    【解决方案1】:

    在这篇文章中,您可以找到可能的错误代码列表,这将帮助您使用特定的错误字符串对其进行分类:Undocumented NSURLErrorDomain error codes (-1001, -1003 and -1004) using StoreKit

    【讨论】:

      【解决方案2】:

      好的,在 didFailWithError 方法中

      if (error.code==NSURLErrorTimedOut) {
      
            errorReason=@"URL Time Out Error ";
      
         }
          else if (error.code==NSURLErrorCannotFindHost) {
            errorReason=@"Cannot Find Host ";
      
         }
             else if (error.code==NSURLErrorCannotConnectToHost) {
                 errorReason=@"Cannot Connect To Host";
      
          }
                   else if (error.code==NSURLErrorNetworkConnectionLost) {
                     errorReason=@"Network Connection Lost";
      
             }
                         else if (error.code==NSURLErrorUnknown) {
                              errorReason=@"Unknown Error";
      
                   }
                                     else {
                                         errorReason=@"Loading Failed";
      
                         }
       UIAlertView *errorAlert=[[UIAlertView alloc]initWithTitle:errorReason message:@"Redirecting to the server failed. do you want to EXIT the app"  delegate:self cancelButtonTitle:@"EXIT" otherButtonTitles:@"RELOAD", nil];
      
      
        [errorAlert show];
        [errorAlert release];
      

      【讨论】:

      • 你看到所有那些“else if”了吗?这就是发明“switch”语句的原因
      • 或者更好的是 enum myEnum { LoadingFaild = -1 } Dictionary dict = new Dictionary ();; dict.Add (-1, "LoadingFailed"); Console.WriteLine("加载错误 = {0}", dict [-1]);
      • 或 dict[myEnum.LoadingFailed]
      猜你喜欢
      • 2011-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多