Reputation: 2386
I am grabbing images from instagram and am receiving urls like "http://instagram.com/p/1JiWygQUSu/media/?size=l" the issue is this url directs to "https://scontent-lax3-1.cdninstagram.com/hphotos-xfa1/t51.2885-15/e15/1527608_451589571655110_1239072675_n.jpg" I need the file extension to properly cache the image on a user device.
The first url is redirecting to the second. I need a way to retrieve the second url from the first.
Tried using this code but it just returns the original url:
let urlString = "http://instagram.com/p/1JiWygQUSu/media/?size=l"
let request: NSMutableURLRequest = NSMutableURLRequest(URL: NSURL(string:urlString)!, cachePolicy: NSURLRequestCachePolicy.ReloadIgnoringCacheData, timeoutInterval: 15.0)
request.HTTPMethod = "HEAD"
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue(), completionHandler: {
(response, data, error) in
print("instaResponse \(response?.URL)")
})
Upvotes: 0
Views: 780
Reputation: 1735
Try looking at NSURLSessionTaskDelegate
method
- URLSession:task:willPerformHTTPRedirection:newRequest:completionHandler:
Documentation here.
You should be able to handle the redirect there and cache the image. Alternatively, you can try using SDWebImage or AFNetworking which have image caching built in, and probably handle the redirect case already.
Upvotes: 1