Swift Facebook Image Picker

Florian Gabach
Nov 25, 2016 · 3 min read
Image for post
Image for post

On a monday morning, your boss let you a note : “Hey, what if you add an easy way for the user to chose his profile picture from his Facebook account ? Could probably do it with the Facebook SDK in less than a couple of hours.”

Fine ! Let’s take a look to the amazing Facebook’s documentation, they probably made a native image picker which permit to log the user, retrieve user’s albums and display pictures of each albums.

Image for post
Image for post
Your face when you realize there is nothing in the native Facebook SDK

It turned out there is no easy and simple way to do elegant Facebook image pickers with the native iOS Facebook SDK.

You will need to handle Facebook login, denied photos permission, parse user’s albums from the Graph API, load albums cover, parse album’s pictures, load and cache pictures. What a really painful task !

And all this, like said your boss, in less than 1 or 2 hours.

There is a solution ! 🎉

There are a couple of websites and old libraries which allow it to be done. Only this is always in an old Swift version or with an old Facebook SDK. That is why I decided to make “GBHFacebookImagePicker”, my own Swift Facebook picker and release it to the under the MIT licence.

Image for post
Image for post

Main features

The picker provide a really simple way to ask users for Facebook’s photos permission and display all the Facebook’s account albums. When albums pictures are displayed the user can easily choose one of them, then the picker download the chosen picture in the highest quality.

Demo

Sometimes users can be strange (or having crazy behaviors) and denied the photo permission. In that case, the picker detect it and ask again for permission.

Image for post
Image for post

The picker provide also a simple way for the popup.

Installation

GBHFacebookImagePicker is available through . , simply add the following line to your Podfile:

pod "GBHFacebookImagePicker"

Usage 🗝

import GBHFacebookImagePicker
  • Implement the GBHFacebookImagePickerDelegate protocol:
// MARK: - GBHFacebookImagePicker Protocolfunc facebookImagePicker(imagePicker: UIViewController, didSelectImage image: UIImage?, WithUrl url: String) {
// Do whatever you whant with the picked image or url ...
}
func facebookImagePicker(imagePicker: UIViewController, didFailWithError error: Error?) {
print(error.debugDescription)
}
func facebookImagePicker(didCancelled imagePicker: UIViewController) {
print("Cancelled Facebook Album picker")
}
  • Display the picker :
let picker = GBHFacebookImagePicker() 
picker.presentFacebookAlbumImagePicker(from: self, delegate: self)

Applications

Some applications already use this picker like :

What about yours ? If your application also use this picker, feel free to contact me or post a comment with its name 😁

What’s next ? 🚀

Customization will be the next focus. With font, color or border customization.

Some people have asked me to make multiple selection features, which probably be one of the next features too.

Feel free to contribute and make pull request on .

If you ❤️️ it, feel free to click on the green heart bellow 👇🏽, or simply ❤️️

If you don’t ❤️️ it say to me why, I will be happy to discuss about it 😁

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store