I have a url from an rss feed that when viewed in a browser, redirects to the full url which contains id's that I need in the app. Is there a way in an app that I can take the simplified url that comes from the feed (i.e. www.somesite.com/Thing1) and essentially forward the link to get the full url back (i.e. www.somesite.com/id=21424255) all within the app.
3 Answers
If the short URL is redirecting to the full URL, NSURLConnection handles the redirect transparently. To get the redirect URL, you need to implement
- (NSURLRequest *)connection: (NSURLConnection *)inConnection
willSendRequest: (NSURLRequest *)inRequest
redirectResponse: (NSURLResponse *)inRedirectResponse
on the NSURLConnection delegate. The redirect target URL is [inRequest URL]
Good luck.

- 3,734
- 2
- 22
- 23
You will need to perform an NSURLRequest
to the short URL, and get the response. You'll want it as an NSHTTPURLResponse
- a subclass of NSURLResponse
Presumably if it's doing a 301 redirect, you can read the response headers and get the URL from the "Location" header.
NSDictionary *headers = [myHTTPResponse allHeaderFields];
NSString *redirectLocation = [headers objectForKey:@"Location"];
Take a look at this question for a more detailed example of creating the NSURLConnection
with the request.
Edit: While the above code is true for other header fields, as Adam pointed out below, you will have to use the connection:willSendRequest:redirectResponse:
delegate method to capture the redirect location as the redirect is handled automatically.
First of all there are a 3 ways to find out a new URL connection.(1) NSURLSession (2) NSURLConnection (3) NSURLDownload. I'm using here NSURLConnection to explain you how to do it.
(1) First of all choose your URL, which redirects (I'm choosing here 'http://www.google.com', because it will be redirected to 'http://www.google.co.in/?gws_rd=cr&ei=LEfeUvCeA8aLrQew04GICQ').Now first, Create two properties in .h file as below.
@interface ConfigViewController ()
@property (nonatomic, retain) NSURLRequest *request;
@property (nonatomic, strong, readwrite) NSURLConnection *connection;
@end
(2) Then, synthesize your properties in .m file.
@synthesize request;
@synthesize connection = _connection;
(3) Then, Create your NSURLRequest and NSURLConnection.
- (void)viewDidLoad{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.google.com"]];
assert(self.request != nil);
self.connection = nil;
self.connection = [NSURLConnection connectionWithRequest:request delegate:self];
assert(self.connection != nil);
}
assert will make sure that your prepared request won't be nil, if it will be nil then it will show an error on that place only, then you have to make sure that you are following this code properly.
(4) Now, implement these two delegation in your .h file - NSURLConnectionDelegate & NSURLConnectionDataDelegate.
@interface WTViewController : UIViewController <NSURLConnectionDelegate, NSURLConnectionDataDelegate>{
IBOutlet UITextView *txtPreviousLbl;
IBOutlet UITextView *txtNextlbl;
}
(5) Then, Implement these 3 delegation methods to your .m (implementation) file.
- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectedResponse{
NSURLRequest *redirectedRequest = request;
if (redirectedResponse) {
redirectedRequest = nil;
}
NSLog(@"RedirectedRequest:%@",redirectedRequest);
return redirectedRequest;
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
NSLog(@"error:%@",error);
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
NSLog(@"Response:%@",response);
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
NSDictionary *responseDictionary = [httpResponse allHeaderFields];
NSString *strNewURLLocation = [responseDictionary objectForKey:@"Location"];
NSLog(@"ResponseDictionary:%@",[responseDictionary objectForKey:@"Location"]);
[[NSUserDefaults standardUserDefaults] setObject:strNewURLLocation forKey:@"serverurl"];
NSLog(@"NewURLLocation:%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"serverurl"]);
}
Here you will get your new URL in [[NSUserDefaults standardUserDefaults] objectForKey:@"serverurl"], You can use a NSUserDefaults or NSString to user in your whole project. Note: Please don't forget to do this code only in UIViewController. otherwise can be possible your delegation won't work in other object files. Have a happy code...

- 6,362
- 1
- 36
- 52