I'm writing a simple Twitter app for OS X and I'm currently stuck on downloading the user's profile image. This is the code responsible for downloading the image:
let imageRequest: NSURLRequest = NSURLRequest(URL: NSURL(string: avatarURL)!)
let session = NSURLSession(configuration: NSURLSessionConfiguration.defaultSessionConfiguration())
let dataTask = session.dataTaskWithRequest(imageRequest) {
(data, response, error) in
println(error)
if data != nil {
println("Got image")
let image = NSImage(data: data!)
completion(image)
} else {
println("Data is nil")
completion(nil)
}
}
dataTask!.resume()
avatarURL
is a String containing this: https://pbs.twimg.com/profile_images/618168482086129664/C0E92y7G.png, which is the URL to my Twitter avatar. As you can see, it's a perfectly valid URL leading to a perfectly valid image.
But whenever I run my app, the println(error)
line prints this into my console:
Optional(Error Domain=NSURLErrorDomain Code=-1002 "The operation couldn’t be completed. (NSURLErrorDomain error -1002.)" UserInfo=0x61000006ea80 {NSErrorFailingURLStringKey=Optional(https://pbs.twimg.com/profile_images/618168482086129664/C0E92y7G.png), NSUnderlyingError=0x600000243720 "The operation couldn’t be completed. (kCFErrorDomainCFNetwork error -1002.)", NSErrorFailingURLKey=Optional(https://pbs.twimg.com/profile_images/618168482086129664/C0E92y7G.png)})
Am I doing something wrong? I've already googled (what a word) this, and almost every single SO page I've found has a solution that basically amounts to "Your URL is missing an URL scheme/has an unsupported URL scheme", which is not the problem I'm having.