iOS Swift: Trouble getting JSON list of photo assets
Hi all! I'm developing an iOS application in Swift whereby I am creating a photo slider that presents an image from a group of images and changes that image out every 5 seconds. The images are hosted on Cloudinary and all are tagged.
To make this happen, I need to pull a JSON list of my photos. I have read the documentation on getting a JSON list, but the iOS example doesn't make sense to me. The documentation says to use this line of code:
imageView.cldSetImage(cloudinary.createUrl().setType( "list").generate("logo.json")!, cloudinary: cloudinary)
I'm not understanding how this line of code actually generates a JSON list. "imageView" in the line of code above is an imageView object. "cldSetImage" seems to want to SET the image asset to the object.
I've tried creating a variable data that would result (hopefully) in the JSON list that's supposed to be returned. Then I used variable "json" set as JSONSerialization.jsonObject to serialize the contents of variable "data".
let data = imageView.cldSetImage(cloudinary.createUrl().setType( "list").generate("logo.json")!, cloudinary: cloudinary) let json = try? JSONSerialization.jsonObject(with: data, options: [])
But this doesn't work. The line of code executes but data is blank, no result.
How can I get a list of photo assets? An JSOn list would be ideal, but even if I can just get a list of the file names that would work, too.
Thanks!
Answers
-
Hi,
I think the imageView.cldSetImage part of that example is incorrect and may have been copied from an example where an image URL is built and passed to the view. The rest of it ( cloudinary.createUrl()….) looks OK, though.Can you please try creating the .json URL with cloudinary.createUrl(), then requesting the URL and seeing if you get the expected result, which is a JSON file containing basic metadata of assets which were tagged with "logo"?
Regards,
Stephen1 -
Unfortunately, this is not working. Here is the code I'm using:
let config = CLDConfiguration(cloudName: "cloudinary://MY_API_KEY:MY_API_SECRET@CLOUDNAME”)
let cloudinary = CLDCloudinary(configuration: config)
let url = URL(string: cloudinary.createUrl().setType( "list").generate(“logo.json")!)
URLSession.shared.dataTask(with: url!, completionHandler: { (data, response, error) -> Void inif error == nil && data != nil {
do {// Convert NSData to Dictionary where keys are of type String, and values are of any typ let json = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers) as! [String:AnyObject]
print(json)
} catch {
print(error)
}
}
else if error != nil
{
print(error)
}
}).resume()
This is the output I get:
response = 0x0000600000a68260
error = NSErrordomain: "NSCocoaErrorDomain" - code: 38400x00006000004e0510
_userInfo__NSSingleEntryDictionaryI *1 key/value pair0x0000600000a11c60
value__NSCFConstantString *"Unable to parse empty data."0x000000010ef1dd880 -
Thanks for the reply, Stephen! I think I did finally get this to work.
0 -
Hey @callisto50. That's great to hear - thanks for letting us know!
In case other users with a similar problem land here from a Google search, would you mind sharing the steps you followed to fix the issue?
Kind regards,
-Danny0 -
Thanks for the response! Unfortunately it isn't working. Here's the code I'm using:
let config = CLDConfiguration(cloudName: "cloudinary://MY_API_KEY:MY_API_SECRET@CLOUDNAME”)
let cloudinary = CLDCloudinary(configuration: config)
let url = URL(string: cloudinary.createUrl().setType( "list").generate(“logo.json")!)URLSession.shared.dataTask(with: url!, completionHandler: { (data, response, error) -> Void in
// Check if data was received successfully
if error == nil && data != nil {
do {
// Convert NSData to Dictionary where keys are of type String, and values are of any type
let json = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers) as! [String:AnyObject]
print(json)
} catch {
print(error)
}
}
else if error != nil
{
print(error)
}
}).resume()
This is the output I get:response = 0x0000600000a68260
error = NSErrordomain: "NSCocoaErrorDomain" - code: 38400x00006000004e0510
_userInfo__NSSingleEntryDictionaryI *1 key/value pair0x0000600000a11c60
value__NSCFConstantString *"Unable to parse empty data."0x000000010ef1dd880 -
What URL are you accessing to download the JSON list? Does that URL return the expected data when you request it in a browser?
Are you certain that there are assets in your account tagged with the "logo" tag?
0 -
Unfortunately, this does not work. Here's my code:
let config = CLDConfiguration(cloudName: "cloudinary://MY_API_KEY:MY_API_SECRET@CLOUDNAME”)
let cloudinary = CLDCloudinary(configuration: config)
let url = URL(string: cloudinary.createUrl().setType( "list").generate(“logo.json")!)
URLSession.shared.dataTask(with: url!, completionHandler: { (data, response, error) -> Void in
// Check if data was received successfully
if error == nil && data != nil {
do {
// Convert NSData to Dictionary where keys are of type String, and values are of any typelet json = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers) as! [String:AnyObject]
print(json)} catch {
print(error)
// Something went wrong}
}
else if error != nil
{print(error)
}}).resume()
This is the output I get:
response = 0x0000600000a68260
error = NSError domain: "NSCocoaErrorDomain" - code: 3840 0x00006000004e0510
_userInfo __NSSingleEntryDictionaryI * 1 key/value pair 0x0000600000a11c60
value __NSCFConstantString * "Unable to parse empty data." 0x000000010ef1dd880 -
Can you please share the exact URL created by your code, and what error, if any, you receive when accessing that URL? Are there certainly assets in your account tagged with the "logo" tag from this example?
The most likely explanations for an error here are that the URL generated was not correct, that the list type is restricted in your account, or that the URL was correct but there are no matching assets and your code isn't able to handle the HTTP 404 response when accessing a non-existing list.If the URL is accessible and returns a 200 response with the JSON list of assets inside, the next most likely explanation is that there's an issue with the part of your code that reads and parses the JSON
0