Hey everybody this is Kayla with slopes.
And in this video we're going to start downloading information from the flicker API.
We just got our API key in the last video and we wrote a function that can build us a nice u r l that's
perfect for accessing the API.
But in this video we're going to go ahead and retrieve a list of the you are Elle's for all of the photos
that we're going to download.
Then in the next video we're going to write another function to retrieve all of the images.
I wanted to separate these two because they're a little beefy and we're going to be diving through some
of the Jaison parsing through it and it can get a little intense.
So I just wanted to separate these functions into two separate videos.
So let's let's get started.
Pull open that X code project and we're actually going to start by going to our map Visi and we're going
to write two functions one for retrieving all of you or else that will be this video.
And then in the next video we're going to write retrieve images which is going to download all the images
using Alamo fire image.
Very cool.
Let's go ahead and at the very bottom of our class here let's go ahead and write phunk retrieve you
ourselves and we're going to retrieve you or else for an annotation.
Right because we drop the annotation on our map view.
And when we drop that annotation it has coordinates and we need to pass those in.
So go ahead and type retrieve your elves for annotation.
Give it an internal parameter of annotation and that is of type drop a pin our custom annotation.
Now we're also going to use a completion handler because we need to know when we're done loading all
of these you are else to call retrieve images because once that's done once they're done then we need
to notify retrieve images that it should start downloading images from those you or else.
OK.
So to make a completion handler just go ahead and type handler and it's going to be of type escaping.
K that means that the values can escape the ones that we pass into this handler can escape out of this
function and talk to another function in our circumstance.
Now we need to put a function in here because that's how completion handlers work is that we're basically
going to pass in values to this function and then we can pass that down as soon as it's done to the
next function or the next.
The next thing that we need to do so.
Handler escaping and then inside of here we're going to put an underscore status of type bool.
OK that's the function.
And then we're going to return an empty an empty parentheses.
So we're not we're not returning anything.
But you'll notice oh it's giving me an error we need to of course put curly brackets and that is our
function that's the base of our function.
Now you're wondering why are we putting this underscore here.
Watch what happens if I get rid of it if I try to build it it's going to give me an error and say function
types cannot have argument labels.
So we have to use an underscore because we're not allowed to use the name for whatever reason.
So now here is what we are going to do.
We're going to first set up an array that's going to hold all of the image you are else.
So go up to the top and we are going to create an array.
Let's see where should we put it.
Let's put it down here below the collection view.
And we're going to call it image you Arel array.
So go ahead and create an instance of it called var image your L array.
And that's going to be of an array of type string and we're of course going to instantiate that as an
array of type string.
Ok image you are LRA.
Very cool.
Now when we go back to our function here retrieve you or else when we first start we want to close that
out.
We want to clear it out so that it is empty.
So go ahead and type image Yoro and make it empty.
This is because if let's say we downloaded all the images downloaded all your files I mean and then
we double tap to drop a new pin.
Suddenly we would be adding a bunch of your L's on top of the ones we already have.
So we should clear it out every time that we call this.
OK.
Are you ready.
We are about to move on to use Alamo fire.
Elmo's Fire is how we're going to make a request and pull down data as Jason.
So follow me here.
We're going to type Alamo fire.
Oh of course we need to import L.A. fire first.
We haven't done that yet.
Import the library Alamo fire in port Alamo fire.
And you know while we're at it go ahead and import Alamo fire image and then scroll back down to where
we were in retrieve you ourselves.
OK.
Go ahead and type.
Alamo fire dot request.
And we're going to choose the one with our El convertible.
Now your old convertible allows us to pass in a string and this is where we're going to pass in our
flicker you are el.
So go ahead and call it flicker Jaro for API key we can pull API keys straight from our Constans file.
The annotation here is going to be of course the annotation from our function and number of photos we're
going to go ahead and let's just do 40.
OK.
So there's that.
Our request has been made but we need to do something with this request.
Now we need to go ahead and see what we're going to get as a response.
So go ahead and type response Jason.
OK.
And this means that we want the data to be pulled down as Jason data as we saw in the previous video
that's totally possible with the flicker API and so we're going to do it.
So for the completion händler just press enter and we're basically going to be looking at the response.
So go ahead and type response and this constant is going to hold the value of whatever Jaison we get
back.
So now we need to actually print the response just to see what we get.
All right.
That's that we're going to print the response we're going to see what we get and then we're going to
parse through it so that we can get all of these nice image you are Elle's ready for us.
So let's go ahead and let's call this function from somewhere.
I think the best place we could call it would be from where we drop our pins so let's go to drop pan
Let's go ahead and at the very bottom.
Go ahead and call retrieve your else for annotation past the annotation that we make when we dropped
the pin and for the handler we're going to go ahead and press enter.
And if you saw the status here is a Boolean that means when it's finished we're going to call that.
OK.
Easy peasy.
And you know what.
At the end why don't we call it so that it just knows that it's done and that's when we'll know it's
finished.
OK.
So we'll just say true.
And in the code We'll just print or no you know we don't need to do anything actually when this runs
it's just going to go ahead and print the response here.
So let's go ahead and build and run this let's drop a pin and that will allow us to see what is getting
downloaded from the Jason K we're zooming in here we go double click and it looks like hey hey we got
a bunch of Jason here.
All right look at that.
We have the page the number of pages the items per page and then look at this.
These are all of our photos here.
There is lots of information here.
So each photo has a farm an ID a property for is family is friend is public an owner a secret a server
and a title.
Lots of information for every single photo.
Now I want to show you something.
If you go to flicker dot com and you view any photo on the site you'll notice something interesting
about the URL so let's go ahead and let's go find a photo.
We can look at maybe this one here and let's see if I open the link.
You know what.
It's not going to let me access the photo I don't think.
That's OK though.
Oh yeah.
Maybe download can download it here.
And let's download a large sized image.
I'm going to copy the link actually and then show you what the link looks like.
Now I want you to notice one thing it says here farm one.
Do you see how in these photos it says farm five farm five farm five farm five they're all farm five.
This one's farm six farm nine.
That's the way the you are is structured farm.
And a number of statics liquor dot com slash.
And then it has a server value for 1 5 6 2 8 2 2.
This one is from server 6 7 9.
Then we have an owner.
The owner key is right here.
OK.
And that goes here.
You can pass that right there.
Actually sorry that's the idea the first one is the ID.
The second is the secret.
OK.
And finally Kay is for a large photo and D goes at the end just as a standard part of how the RL is
structured.
So as you can see the URL is pretty complicated to parse and that's why I wanted to make this into a
single video.
I hope you can understand that.
But thank you for being patient with it being just in a single video.
So let's go ahead and let's actually look at what we're dealing with here.
We have a dictionary first and foremost and inside that dictionary is another dictionary called photos.
OK.
Then inside of that dictionary there's an array which we can see here with that print see parentheses.
There's an array of more dictionaries.
OK these are all individual dictionaries for each photo.
So we're going to have to go ahead and parse down through a dictionary which is the Jaison we receive
initially then we're going to cast that as a dictionary and we'll cast that as an array of dictionaries.
OK so let's go do that inside of our Alamo fire function.
We know what the response is.
That is a dictionary.
So let's create a constant called Jaison and let's do this safely guard.
Let Jaison equals response.
And in order to get this Jaison information we can type response the result value ops response that
result value.
And we need to actually cast this as a dictionary of string and any object and I can see that each of
these values here is a string and the object that can be returned can be an integer.
Sometimes it can be a string sometimes it could be maybe a decimal place if it had a coordinate.
So we're going to do string and any object to make sure that it's safe at the end of our guard statement
we'll type.
Else.
And then of course we will return if there is a problem.
So now we're going to use this Jasen which is a dictionary of string and any object we're going to use
that in order to get into the photos dictionary.
So let's go ahead and let's type let photos dep't for dictionary and make that equal to Jaison.
And if you look the name of that element is called photos.
So we're going to actually type photos and that's going to bring out that dictionary.
Now of course we need to cast it as a dictionary of string
and any object.
And why are we doing that.
Well if you look at the Jaison here we can see that Fotos has a little bracket here and inside of this
dictionary there are some keys right and some values.
And for photo photo is the key and the value is an array of a bunch of dictionaries of photos.
OK.
And we're going to get into that in just a moment.
So now we have accessed the photos dictionary and we need to go a little bit deeper it's kind of like
going into different levels of Inception we're going down into the third level of sleep.
Sort of but not nearly as cool.
Actually more cool because it's code and codes cool.
So let's go ahead and do that.
Let Fotos dictionary array because it's an array of photo dictionaries.
We're going to make that equal to photos.
DICT.
And we're going to pull out the element called photo right because that's what we see.
Photo is the key.
And we want the value.
So let's go ahead and call.
Photo from photos dictionary array.
And we're going to cast this as an array of dictionaries that are of type string and any object.
All right.
Easy enough.
Now of course it's telling us that we're not using it yet but we're going to go ahead and cycle through
all of those photos and we're going to generate a u r l based on the information that we have here.
So for photo in photos dict array we're going to use a for loop cycle through all of them.
We're going to first create a post u r l and then we're going to append it to our image you are LRA.
So go ahead and create a constant called Let post you are ill.
And for this we're actually going to go ahead and use a string.
We're going to look at the your L from safari.
We can see it starts with h TTP s.
Backslash backslash.
So let's go ahead and type that.
Great.
Next we have a farm and a number.
So I know that farm can be typed but you know what we're going to need to get out the value from photo
for the farm so to do that we're going to use string concatenation.
We're going to call photo because remember that's its own dictionary and we can pull out the value for
one of the keys and the key is farm when we call farm for this one we get a farm of five.
But you know what we actually need to force unwrap this so that we pull out the value successfully.
So for this example we have to slash farm five k.
And after that we can do dot static flicker dot com.
So let's type that at the end.
Static flicker dot com slash.
And now what.
Six seven nine.
Now I'm going to give you a clue that is actually the server number.
OK I told you that before but that is what it is.
So to get that we're going to use string concatenation again.
I'm sorry string encapsulation.
I always flip those terms and we're going to go to the photo dictionary and we're going to pull out
the value for server force and wrap it as well.
OK so now we have the value for the server number.
So for this example the server number would be 4 to 1 1.
Okay.
Next up we're going to go ahead and pull out the photo ID.
That's like the unique ID of the photo.
So if you look at the you Arel this last part starts with the ID right here.
OK.
So we're going to go ahead and use string encapsulation again to pull out photo and call the ID parameter
and force on rapid.
Wonderful but you'll notice that right after the ID there's an underscore.
OK so let's put that in R U R L and then after that we actually need the secret K and you can see the
secret right there.
So let's do what we've been doing string encapsulation photo whoopsies.
Toe.
And we're going to pull out secret and force and wrap it.
All right.
After that we're going to go ahead and do underscore K.
Let's do that underscore K underscore d dot Jaypee.
And you know what that is r u r l.
That was really messy having to siphon through all of that Jason.
But we now have a post u r l and we know exactly where we can append that image u r l array so let's
do that.
Go ahead and type self image u r LRA.
Append new element post u r l and that is that.
So once we have cycled through all of this image you are all image you are LRA will be nice and full.
And at the end of that call we can use our handler to escape to notify us that we're done downloading
all your else.
So go ahead and type handler whip's handler and pass in a boolean of true so that is done make sure
that you study this and go through it.
If you already have experience with Jaison this might be quite easy for you.
But if not it might take a while to understand what's really going on here.
But let's go back up to where we call this function in dropped pin and I'm actually going to print out
the image you are LRA so you can see all the imagery or else that we've just made.
So go ahead and type print image you are LRA and let's build and run this app.
Oh whoops of course we need to make a reference to self because we're inside of a closure here and retrieve
you or else build and run and we're going to see if this worked if our your L's are being properly downloaded
and pulled out from our Jason File.
So go ahead and double click and look at that.
That looks pretty good.
Let's see farm five static flicker dot com slash for two on one there's the ID secret Cady to double
check that this worked.
I'm going to actually copy one of these into my browser and see if it worked.
Kate says this photo is no longer available so that means we did something wrong.
Let's see if we can figure out what the issue was.
Or alternatively that image may actually just be deleted or not available.
OK so that's not good.
It's telling us that there is a problem farm five static flicker dot com slash server slash ID secret
k d.
So let's go back to that photo we had of our gorilla friend and let's see whoopsies Let's see what the
your L was when we copied the link pasted it here.
Let's take a look.
What did we miss.
Farm one static flicker dot com server ID secret k.d. lang.
Hmm interesting.
So we may have missed a slash here or there.
Let's go ahead and let's head down.
We have the farm number.
We have static flicker dot com.
Is that all right.
Static flicker dot com slash the server number.
OK.
And we have Yeff the farm the server number we have the ID underscore the secret Cady.
You know what.
Let's see there are some other letters HD for instance downloads a medium sized image.
So let's see if it's just an issue with using large images.
And you know what it looks like.
That was the problem because it just downloaded the image here for me in medium size so it looks like
maybe large images are not accessible by the API.
That's good to know.
So let's go ahead and replace k with h.
Let's build it one more time and you should be able to see a giant list of properly formatted you or
else show up right here in our console.
Let's do that.
Here we go double click downloads all those you are else.
Lovely.
And let's go ahead and let's paste this into safari.
And here we go.
All right.
It downloaded.
And here we are.
There's somebody at Disneyland.
Awesome.
So it is properly downloading the images.
We have lots of you or else in the next video we're going to use these you are else to download those
images and save them into our application using Alamo fire image.
Amazing job.
And let's head over to the next one.