网络连接失败时停止下一个进程

时间:2012-12-29 09:16:23

标签: iphone xcode uialertview

我正在从服务网址获取xml响应,我希望在网络连接失败时显示错误。所以我显示了UIAlertView,但是这个alertView在其余的进程完成后会显示出来。我想让它立即显示出来。

在android中,如果网络连接失败,它将显示错误警告“不幸的应用程序名称已终止”。 iPhone上有这样的东西吗?如果不是,我想显示alertview并停止其余的过程。

这是我正在处理的代码:

if (responseData!= NULL)
        {
            response = [[NSString alloc] initWithData:responseData 
                                             encoding:NSUTF8StringEncoding ];
            NSLog(@"Response Code:%d",[urlResponse statusCode]);
            if([urlResponse statusCode ]>=200 && [urlResponse statusCode]<300)
            {
                NSLog(@"Response:%@",response);
            } 
        }
        else
        {
            NSLog(@"Failed to send request: %@", [error localizedDescription]);

              UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Unfornately  stopped.Try Again " message:@"" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
               [alert show];
               [alert release];
        }  

        parser =[[NSXMLParser alloc]initWithData:responseData];
        [parser setDelegate:self];
        [parser setShouldProcessNamespaces:NO];
        [parser setShouldReportNamespacePrefixes:NO];
        [parser setShouldResolveExternalEntities:NO];
        currentHtmlElement=@"1";
        [parser parse];
        [parser release]; 

在我的代码中,如果它无法发送请求,则会打印NSLog并执行alertView代码。同时我想停止剩下的进程,即初始化解析器并完成剩余的操作。

我该怎么做?

2 个答案:

答案 0 :(得分:0)

非常简单......将您的解析代码移到if块内。

    if (responseData!= NULL)
    {
        response = [[NSString alloc] initWithData:responseData 
                                         encoding:NSUTF8StringEncoding ];
        NSLog(@"Response Code:%d",[urlResponse statusCode]);
        if([urlResponse statusCode ]>=200 && [urlResponse statusCode]<300)
        {
            NSLog(@"Response:%@",response);
        }

        parser =[[NSXMLParser alloc]initWithData:responseData];
        [parser setDelegate:self];
        [parser setShouldProcessNamespaces:NO];
        [parser setShouldReportNamespacePrefixes:NO];
        [parser setShouldResolveExternalEntities:NO];
        currentHtmlElement=@"1";
        [parser parse];
        [parser release];
    }
    else
    {
        NSLog(@"Failed to send request: %@", [error localizedDescription]);

          UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Unfornately iCloudBiz has stopped.Try Again " message:@"" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];

           [alert show];
           [alert release];
    } 

顺便说一下......您提到了Android的应用行为,如下所示

  

在android中,如果网络连接失败,它将显示错误警告,表示不幸的应用名称已终止。

请理解此行为是一种异常行为。这称为CRASH,你应该适当地处理它。如果网络断开,应用程序应显示消息而不终止/崩溃:)

答案 1 :(得分:0)

您应该使用Reachability类来检查互联网连接是否可用 这是了解其工作原理的示例代码

Reachability *reachability = [Reachability reachabilityForInternetConnection];    
NetworkStatus internetStatus = [reachability currentReachabilityStatus];
if (internetStatus != NotReachable) {
//my web-dependent code
}
else {
//there-is-no-connection warning
}