Ashok
Ashok

Reputation: 5655

Checking Internet Connection In Between Process in iphone

I have the following code

NSURL *url = [NSURL URLWithString:@"http://wallpaperswide.com/download/flattened_grass-wallpaper-2800x1050.jpg"];  //Line1
NSData *data = [[NSData alloc] initWithContentsOfURL:url];  //Line2
UIImage *tmpImage = [[UIImage alloc] initWithData:data];  //Line3
NSLog(@"%@",tmpImage);  //Line4

In line2 to it will take some time to download image in that url...in between i have to check the internet connection..how it is possible?

i will explain my problem in detail....

upto line1 the internet connection is there for my iphone. now the line2 is executing...while the downloading process is going on , at the middle of this process, i lost my internet connection..

Now how can i notify the user as "You have no internet connection"...

so how can i check the Internet Connection In Between this Process.?

is there any delegate methods for it...please help me

Thanks in advance.....

Upvotes: 1

Views: 434

Answers (6)

Bohrnsen
Bohrnsen

Reputation: 79

You could use the Reachability class modified of Tony Million that you can find at Reachability

You can use this to define blocks which gets invoked when reachability changes. So you can use it to show an UIAlertView.

If you want to download the image asynchronous i would recommend to use AFNetworking. It already provides the possibility to add a ReachabilityStatusBlock to the AFHttpClient used.

So you can rewrite your code if importing AFNetworking.h to:

AFHTTPClient *client = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://wallpaperswide.com"]];
[client registerHTTPOperationClass:[AFImageRequestOperation class]];

[client setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
    if(status == AFNetworkReachabilityStatusNotReachable){
        NSLog(@"Not reachable");
    }
}];

AFImageRequestOperation *operation = [AFImageRequestOperation imageRequestOperationWithRequest:[client requestWithMethod:@"GET" path:@"/download/flattened_grass-wallpaper-2800x1050.jpg" parameters:nil] success:^(UIImage *image) {
    NSLog(@"%@", image);
}];
[operation start];

Upvotes: 1

user2082055
user2082055

Reputation:

Here the possible ways...

1) Using NSURLConnection Delegate Methods.

2) Using Rechability classes

Upvotes: 1

Ashok
Ashok

Reputation: 5655

Finally I found answer for my Question...i'm pasting here for the future reference

Declare a global variable

NSMutableData *receivedData;

In ViewDidLoad:

receivedData=[[NSMutableData alloc]init];
NSURLRequest *theRequest =[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://wallpaperswide.com/download/flattened_grass-wallpaper-2800x1050.jpg"]];


NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

if (theConnection)
{
}

else
{
    // Inform the user that the connection failed.
    UIAlertView *connectFailMessage = [[UIAlertView alloc] initWithTitle:@"NSURLConnection " message:@"Failed in viewDidLoad"  delegate: self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
    [connectFailMessage show];
}

NSURLConnection Delegate Methods..

#pragma mark NSURLConnection methods

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
 {
[receivedData setLength:0];
 }

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[receivedData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    // inform the user
UIAlertView *didFailWithErrorMessage = [[UIAlertView alloc] initWithTitle: @"NSURLConnection " message: @"didFailWithError"  delegate: self cancelButtonTitle: @"Ok" otherButtonTitles: nil];
[didFailWithErrorMessage show];

//inform the user
NSLog(@"Connection failed! Error - %@ %@",
      [error localizedDescription],
      [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
 UIImage *tmpImage = [[UIImage alloc] initWithData:receivedData];
  myImageView.image=tmpImage;
 NSLog(@"%@",tmpImage);
}

Upvotes: 0

Rui Peres
Rui Peres

Reputation: 25927

What I usually do is to use NSURLConnectionDelegate. If there is an error on the connection, I will then use Reachability, to see if there is a problem with the internet, or if it was a server error.

Upvotes: 2

Tung
Tung

Reputation: 21

NSURLConnection NSURLConnectionDelegate NSURLRequest *theRequest=[NSURLRequest requestWithURL:
[NSURL URLWithString:@“http://www.sina.com.cn/”]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
NSURLConnection *theConncetion=[[NSURLConnection alloc]
initWithRequest:theRequest delegate:self];
if(theConnection)
{

receivedData=[[NSMutableData data] retain];
}

  • (void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error

Upvotes: 1

Kaushal Bisht
Kaushal Bisht

Reputation: 486

You can do something like this

Reachability *reach = [Reachability reachabilityForInternetConnection];
NetworkStatus netStatus = [reach currentReachabilityStatus];
if (netStatus == NotReachable) {
    UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"" message:@"Could not connect to the server, please check your internet connection !" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
    [alert show];


}
else
{

 }

}

Upvotes: 2

Related Questions