-5

问题是我有这些消息:

  • 主机未找到
  • 找不到具有指定主机名的服务器
  • 网络不可用

尤其是它显示的第二条消息,然后一切正常

所以我想知道如何找到这些消息以及如何处理它们并放置我的消息而不是它们,或者只是进行其他操作而不是像 facebook 应用程序那样显示它们,当连接断开时它会显示带有标签的红色警报“网络错误”

我怎样才能做到这一点?

4

3 回答 3

0

为什么不在您的代码中使用可达性测试?

您可以从 Apple Developer 门户中找到示例代码。链接:https ://developer.apple.com/library/ios/#samplecode/Reachability/Introduction/Intro.html

代码:

 Reachability *reachability = [Reachability reachabilityForInternetConnection];
    NetworkStatus internetStatus = [reachability currentReachabilityStatus];

    if (internetStatus != NotReachable) {


      // Do Your Stuff As Internet Available        

    } else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"We are unable to make a internet connection at this time. Please try again later" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
        [alert show];


    }
于 2012-12-21T12:04:38.720 回答
0

您可以检查您的错误,然后使用stringByReplacingOccurrencesOfString方法转换您的自定义错误消息

NSString *str = @"server with a specified hostname could not be found";

if ([yourErrorMsg isEqualToString:str])
{
    NSString *customError = [yourErrorMsg stringByReplacingOccurrencesOfString:str withString:@"Your Custom String"];
    NSLog(@"New custom error = %@",customError);
}
于 2012-12-21T14:19:36.980 回答
0

最后我找到了解决方案,就像这样

-(void)handleError:(NSError*)error{
SEL onerror = @selector(onerror:);
if(self.action != nil) { onerror = self.action; }
if([self.handler respondsToSelector: onerror]) {
    if (error.code == -1003 || error.code == -1001 || error.code == -1004) { //check all kind of errors
        [self send];
    }else{
    [self.handler performSelector: onerror withObject: error];
    }
} else {
    if(self.defaultHandler != nil && [self.defaultHandler respondsToSelector:onerror]) {
        [self.defaultHandler performSelector:onerror withObject: error];
    }
}
if(self.logging) {
    NSLog(@"Error: %@", error.localizedDescription);
}

}

于 2012-12-21T17:23:05.760 回答