14

i am sending an object to this adrees : https://sandbox.itunes.apple.com/verifyReceipt

with NSUrlconnection and i am trying to read it with this delegate method :

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response like this :

NSlog(@"%@",response); i am getting this code :

<NSHTTPURLResponse: 0x7d2c6c0> i need to get a string somehow. how can i read it?

James Webster
  • 31,873
  • 11
  • 70
  • 114
orthehelper
  • 4,009
  • 10
  • 40
  • 67

4 Answers4

19

I wrote this answer to another question, but I think it will help you. Have a look in particular at the methods

-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data

and

-(void) connectionDidFinishLoading:(NSURLConnection *)connection


-(void) requestPage
{
    NSString *urlString = @"http://the.page.you.want.com";
    NSURL *url = [NSURL URLWithString:urlString];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLCacheStorageAllowed timeoutInterval:20.0f];


    responseData = [[NSMutableData alloc] init];
    connection = [[NSURLConnection connectionWithRequest:request delegate:self] retain];
    delegate = target;
}


-(void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{   
    if ([response isKindOfClass:[NSHTTPURLResponse class]])
    {
        NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*) response; 
        //If you need the response, you can use it here
    }
}

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

-(void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    [responseData release];
    [connection release];
}

-(void) connectionDidFinishLoading:(NSURLConnection *)connection
{
    if (connection == adCheckConnection)
    {
        NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];

        //You've got all the data now
        //Do something with your response string


        [responseString release];
    }

    [responseData release];
    [connection release];
}
James Webster
  • 31,873
  • 11
  • 70
  • 114
  • but how to address multiple requests?? for example when a connection is going on and another request is coming before the connectiondidfinishloading then chances are that the responseData may get released right?? – raghul Mar 17 '13 at 15:19
  • @raghul Yes, that's possible. To circumvent that, I'd probably wrap this in a class "ConnectionHandler" so that each connection would have it's own instance of `responseData` – James Webster Mar 18 '13 at 08:21
6
NSHTTPURLResponse * httpResponse = (NSHTTPURLResponse *) response;
int errorCode = httpResponse.statusCode;
NSString *fileMIMEType = [[httpResponse MIMEType] lowercaseString];

For more information, check iOS Docs : NSHTTPURLResponse.

And be patient: not all connections return NSHTTPURLResponse

Nekto
  • 17,837
  • 1
  • 55
  • 65
3

If you expect that the connection is receiving some data you can use.

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data

you can then simply convert data to NSString.

Himanshu A Jadav
  • 2,286
  • 22
  • 34
1

You can create a subclass and override the - (NSString*) description method.

Volodymyr B.
  • 3,369
  • 2
  • 30
  • 48