Reputation: 3433
I am new to web services. I need to get information from .NET web server. For that I am using the following code:
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[webData setLength: 0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[webData appendData:data];
if (data) {
NSLog(@" bcxbm,xcm,xcmcmx,b data consist %@",data);
}
else {
NSLog(@"not exist");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"UIAlertView" message:@"<Alert message>" delegate:self cancelButtonTitle:@"Retry" otherButtonTitles:@"Cancel", nil];
[alert show];
[alert release];
}
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"ERROR with theConenction");
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"DONE. Received Bytes: %d", [webData length]);
I am able to get information, but I need to validate it. If there is no connect, I need to fire an Alertview
. For that I have the code:
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[webData appendData:data];
if (data) {
NSLog(@" bcxbm,xcm,xcmcmx,b data consist %@",data);
}
else {
NSLog(@"not exist");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"UIAlertView" message:@"<Alert message>" delegate:self cancelButtonTitle:@"Retry" otherButtonTitles:@"Cancel", nil];
[alert show];
[alert release];
}
}
I check by removing connection. But it does not fire any alerts. After some time, it displays Error in connection
in console. But it takes much time. Is there any other possibility than this? How to display alert when there is no connection?
Can anyone please help me?
Upvotes: 3
Views: 435
Reputation: 10548
connection didFailWithError:
would be called when connection could not be established. If connection didReceiveData:
is being called, it means that you are receiving some kind of data from the web service.
When you remove the connection, your code tries to connect to the web service and times out after a predefined interval, that is when connection didFailWithError:
gets called. It takes time as it tries to connect until the timeout occurs.
You could check for the internet connectivity before even setting up a connection using Apples's Reachability example. You can see how to use it from this link
Upvotes: 1