Question

I'm using Apple Document to create an app.I succeed in connection to the server, but I receive 0 bytes from the server (no response data). I take the following steps:

  1. I create a view-based App and add a property 'receivedData':

    In ViewController.h:

    @property (nonatomic, retain) NSMutableData *receivedData;

    In ViewController.m:

    @synthesize receivedData; 
  2. ViewController.m's action 'ViewDidLoad', I add:

    receivedData = [NSMutableData alloc];
  3. Add a button in the View and add action for it:

    // create the request
    NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://..."]
                        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 that will hold
    // the received data
    // receivedData is declared as a method instance elsewhere
    receivedData=[[NSMutableData data] retain];
    } else {
    // inform the user that the download could not be made
    }

When I debugging these codes, I find that receivedData returns 0 bytes. Any ideas about what goes wrong? A simple modify of my code will be appreciated.

Was it helpful?

Solution

Your code only creates the HTTP connection - the data will only be written to and available in receivedData after the delegate callbacks have been called by the framework (once the HTTP response is received). You can get more information and sample code from Apple's documentation

OTHER TIPS

The answer is the same as it was the last time I answered it for you, over at How can I receive data from URL on iPhone?. I gave a detailed explanation-- did you read it?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top