代码之家  ›  专栏  ›  技术社区  ›  Dilshan

iPhone应用程序-与服务器通信时的错误处理

  •  0
  • Dilshan  · 技术社区  · 15 年前

    我的iPhone应用程序不断地与web服务器通信。因此,如果连接丢失或无法访问服务器,我们是否应该采取特定的预防措施?我知道我们必须向用户展示通信失败的情况。如果是,是否有任何可接受的方式向用户显示错误?我更专注于苹果的指导方针,在那里我读到他们拒绝应用程序,因为这个问题。

    当做,

    帝山

    3 回复  |  直到 15 年前
        1
  •  1
  •   gabaum10    15 年前

    只需使用apple可达性类。 Here here . 如果您失去与服务器的连接,则只需检查可访问性并向应用程序中添加侦听器即可进行更新。

    一旦你的项目中有了可达性类,你所要做的就是这样:

    //Change the host name here to change the server your monitoring
    
    hostReach = [[Reachability reachabilityWithHostName: @"www.apple.com"] retain];
    [hostReach startNotifier];
    
    
    internetReach = [[Reachability reachabilityForInternetConnection] retain];
    [internetReach startNotifier];
    
    wifiReach = [[Reachability reachabilityForLocalWiFi] retain];
    [wifiReach startNotifier];
    
    NetworkStatus netStatus = [internetReach currentReachabilityStatus];
    
    if (netStatus == NotReachable){
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Network Connection Not Found" message:@"Need network connection present to operate."
                                                       delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
        [alert release];
    }
    

        2
  •  1
  •   Pierre    15 年前

    在发送请求之前,您应该检查服务器是否可访问,如果发生错误或服务器无法访问,您可以显示UIAlertView来通知用户。

        3
  •  1
  •   EricS    15 年前

    我发现与其检查可达性,不如尝试访问网络,因为可达性可以在DNS查找中锁定20秒以上,而NSURLDownload有时会在网络关闭时打开网络。

    但是,您仍然应该使用可达性在网络关闭后重新联机时重试。