Work with short URLs using HTTP redirects. You can configure NSURLConnectionwith a request with a short url and set a delegate. In deletet, execute this method:
- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response;
response nil, . . , NSHTTPURLResponse -isKindOfClass:, .
, request, NSURLConnection. URL URL-, .
, , , , , -cancel .
Update. :
#import <Foundation/Foundation.h>
static BOOL stop;
@interface MyDelegate : NSObject
@end
@implementation MyDelegate
- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response
{
NSLog(@"request %@", request);
NSLog(@"response %@", response);
return response ? nil : request;
}
- (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"%s: error %@", __func__, error);
stop = TRUE;
}
- (void) connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"%s", __func__);
stop = TRUE;
}
@end
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSURLRequest* req = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://tinysong.com/HJ9h"]];
MyDelegate* del = [[MyDelegate alloc] init];
[NSURLConnection connectionWithRequest:req delegate:del];
while (!stop)
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
[pool drain];
return 0;
}
:
request <NSURLRequest http:
response (null)
request <NSURLRequest http:
response <NSHTTPURLResponse: 0x103500340>
-[MyDelegate connectionDidFinishLoading:]