0

I'm using this code:

NSString *recievedData;

NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.site.com/"]
                                          cachePolicy:NSURLRequestUseProtocolCachePolicy
                                      timeoutInterval:60.0];
// create the connection with the request
// and start loading the data
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection) {
    // Create the NSMutableData to hold the received data.
    // receivedData is an instance variable declared elsewhere.
    recievedData = [NSMutableData data];

    NSLog(@"%@", recievedData);
} else {
    // Inform the user that the connection failed.
    NSLog(@"Unsuccessful.");
}

It's a modified version of this.

My problem is that it always returns

<>

Whether I'm connected to the internet or not, it always goes to successful.

citruspi
  • 6,709
  • 4
  • 27
  • 43

2 Answers2

0

NSURLConnection doesn't work that way. You start a connection and then receive callbacks as data is received.

If you want a simple call to retrieve remote data, use NSData's dataWithContentsOfURL method. However, you should only use that on secondary threads because otherwise it will lock up your user interface for the duration of the call and the system may terminate your app if it takes too long.

See the full code at NSURLConnection example.

EricS
  • 9,650
  • 2
  • 38
  • 34
0

You haven't received any data, you have just instantiated the object that will hold the received data. You need to implement the delegate methods for handling responses and failures and it is usually best to use NSURLConnection asynchronously.

There is some example code Using NSURLConnection

MikeJ
  • 542
  • 6
  • 19