Hello Everyone.
I finally got the time to write after a few months of being really busy. I hope everyone would be doing good.

So I was working on this FlickrKit a while back and at that time I decided to write about it, on how to use it with Swift.

Setting Up App on Flickr.com

In order to be able to use Flickr API you would need to get API credentials from their website. Open the link. Click on Request an API key.

It will take you to the following screen. Click on Request API key again.

Then we will have to specify Flickr if the app is for commercial use or for personal. Since its a demonstration I will select Apply for Non-Commercial Key.

Tell Flickr about the app, then Submit.

After that your keys will be generated, and you’ll be redirected to the following screen, then click on Edit auth flow for this app.

 

FlickrKit is a 3rd party library which uses Callback Url for authentication of user and providing token. So for that reason we will have to specify a Callback url and set Application type to Web Application. Callback url can be xyz://auth. then click save changes. After that you’re done setting up.

 

Setting up Flickr Kit in Project

Create a project and initiate a pod in it, and install. Since FlickrKit is written in Objective-C. You’ll have to import it in your bridging-header as well.

Now in your AppDelegate. import FlickrKit and initialize your Flickr app with the following code in didFinishLaunchingWithOptions:

After that open your Info.plist file and add the following keys.

Open your Storyboard and add a View Controller and in your View Controller add a WebView and make the delegate self to the View Controller and create and outlet, conform to protocol UIWebViewDelegate.

We’ll write a method which will open Flickr inside of our webview. Call this method in viewDidLoad or viewWillAppear.

And in your UIWebViewDelegate method shouldStartLoadWith do the following. Don’t forget to change the string ‘flickrphotoviewer’ to the prefix of your callback url.

What this does is get User’s token if user allows the permission and returns to main screen otherwise. Once you get the token, save it in UserDefaults.

I have created a class called FlickrHelper, with the reusable helper functions in it.

The function checkAuthentication takes in user token and provides their info in return. Where as the login function gets the user login if token is present.

We will have to save the userId because it will be required in order to get user’s pictures from their profile.

Getting Photos

From User Profile

In order to get photos from user’s picture. Copy and paste the following code in your View Controller where you want the pictures to be displayed. FlickrKit returns photo URLs.

We can use any 3rd party image library like SDWebImage or MapleBacon to download the images from.

 

From Photos Stream

This function is hardcoded to get only first 30 photo URLs. You can further change it and use pagination and get photos per page.

Build and runs your app and make sure everything is working.

I have created a sample project incase if you people find any difficulties. You can find the project here.

If you guys have any questions please leave a comment. 🙂

Share:

administrator

Aaqib is an enthusiastic programmer with the love of Swift and anything that looks like Swift i.e Kotlin. He loves writing code in Swift, and exploring new technology and platforms. He likes to listen to old music. When he is not writing code, he's probably spend his time watching movies, tv-shows or anime, or either doing some research for writing the next article. He started Kode Snippets in 2015.

Leave a Reply

Your email address will not be published. Required fields are marked *