Using Instagram in your iOS App: Consuming the Instagram Basic API and Instagram Graph API in July 2020

Congratulations to Enhance IT for launching their Instagram page! In celebration, wouldn’t it be fun to be able to pull the photos down from one’s Instagram page? I think so, thus, I present to you, iOStagram: a very simple app to perform this task.

EnhanceIT.png

iOStagram is extremely straightforward. You can sign in with your Instagram account and see a gallery of your photos. I’ve also added a gallery view of Enhance IT’s photos.

While Instagram has documentation for using their Basic Display API, for authentication, and Instagram Graph API, for accessing user information, I thought it would be more fun to build a framework around this such that, you too, would be able to let your users sign into Instagram and retrieve their photo information.

To accomplish these tasks, I’ve gone and wrapped the Instagram’s Basic and Graph APIs within a framework called InstagramBD, a very simple framework for using accessing these Instagram APIs, for iOS, written in Swift.

Using InstagramBD is also very simple.

 

Step 1. Creating a Credentials instance.

InstagramBD.png

Credentials are used for the Basic API. In here, you’ll want to enter your Instagram App ID for your ‘key’ and Instagram App Secret for your ‘secret’

The Basic Display API does not support URL schemes and so, I’d provided a direct link to my GitHub account, myself. If you have an actual server, that is also fine, in lieu of an arbitrary https://.

 

Step 2. Start the Sign In Flow

InstagramBD2.png

IGLoginViewController is a very straightforward object. It is a UIViewController, wrapping a WKWebView, and all it needs are your Credentials. Once the user authenticates, you will receive a Result type, with an InstaUser instance.

Here, I’ve opted to send a user into the gallery, once the user has been made.

 

Step 3. Accessing a user’s information.

InstaUser is our most important object in InstagramBD. It can access the different endpoints available from Instagram’s Graph API.

InstagramBD3.png

 Here, we can receive the screenname and media information of our signed-in user.

 In addition, it also fully encapsulates the complexities allowing multiple tries for an API call in cases of random API failures, Oauth token refresh (in development, as of writing), and securing a user’s Oauth token within the Keychain (including clearing the Keychain on new installs).

 Since the user’s token is kept in the Keychain, you can also retrieve this information, and if it is still valid, immediately send your user to the signed-in experience immediately, just by making an InstaUser yourself, with your Credentials.

Picture1.png

In closing.

 That’s about the long-and-short of it. If you would like to see more about using Instagram’s APIs, or using InstagramBD (or some part there-of), feel free to visit its repository.