Receiving data from URL on iPhone?
-
09-09-2019 - |
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:
I create a view-based App and add a property 'receivedData':
In ViewController.h:
@property (nonatomic, retain) NSMutableData *receivedData;
In ViewController.m:
@synthesize receivedData;
ViewController.m's action 'ViewDidLoad', I add:
receivedData = [NSMutableData alloc];
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.
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?