Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check what URL your NSURLConnection is giving you a response from?

Before iOS5 I could test to see what URL I was getting a response from my code looked like this:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{
    returnString = [[[NSMutableString alloc] initWithData:responseData encoding: NSUTF8StringEncoding] autorelease];
    NSString * currentParseString = [NSString stringWithFormat:@"%@",connection];
    NSLog(@"Currently Parsing: %@",currentParseString);
}

my log would print out "Currently Parsing: http://www.myinfo.com/parser...."

Which I could then use to test and send into different IF statements. My problem now is, in iOS5 the connection is no longer printing as a URL, it is printing as a block of memory <NSURLConnection: 0x6a6b6c0> How could I get it to print out as a URL again so I dont have to rewrite my IF statements?

like image 243
Louie Avatar asked Dec 16 '11 02:12

Louie


2 Answers

I did something like this :

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{
   NSLog(@"%@",[connection currentRequest]);
}
like image 194
Maulik Avatar answered Nov 14 '22 23:11

Maulik


When you put %@ within a NSString format it will be replaced by [object description]. Apple must have changed what [connection description] returns. It's hard to rely on that in any case as it's not guaranteed to be in any particular format.

There are a number of ways you could change your implementation. Probably the quickest is to create a very simple subclass of NSURLConnection along the lines of:

// MyNSURLConnection.h
// code written assuming ARC
@interface MyNSURLConnection : NSURLConnection
@property (nonatomic, strong) NSURL *requestURL;
@end


// MyNSURLConnection.m
// example override, you can override all the init/connection methods
- (id)initWithRequest:(NSURLRequest *)request delegate:(id)delegate startImmediately:(BOOL)startImmediately {
    self = [super initWithRequest:request delegate:delegate startImmediately:startImmediately;
    if (self) {
        self.requestURL = request.URL;
    }
    return self;
}


// in your MyNSURLConnectionDelegate controller
- (void)connectionDidFinishLoading:(MyNSURLConnection *)connection {
    returnString = [[[NSMutableString alloc] initWithData:responseData encoding: NSUTF8StringEncoding] autorelease];
    NSString * currentParseString = [NSString stringWithFormat:@"%@",connection.requestURL];
    NSLog(@"Currently Parsing: %@",currentParseString);
    // rest of your code
}
like image 40
XJones Avatar answered Nov 14 '22 21:11

XJones