JPMartha's Pancake

This blog is the way to brush up my poor English.

Give an Image to Instagram app in Swift

I'm Martha.

I write an article in this blog for the first time.

This is based on the following my article. qiita.com

I'd like to brush up my poor English. If there are some mistakes in my English, please feel free to correct them.

Overview

This article describes the Instagram's sharing flow in Swift.

f:id:JPMartha:20160109223230g:plain

It doesn't describe the way to post photos on Instagram directly using the Instagram API.

Motivation

  • I use Instagram on my personal life, so I tried coding.
  • It seems that few libraries have only this function.
  • I'd like to introduce my library.

Getting started

Save the file in PNG or JPEG (preferred) format. Two combinations of filename extension and UTI is given. I recommend the latter, because of displaying unnecessary icons.

filename extension UTI
.ig com.instagram.photo
.igo com.instagram.exclusivegram

Use the UIDocumentInteractionController to give it to Instagram app.

Example

On your application Info.plist, add the LSApplicationQueriesSchemes key.

Key Type Value
LSApplicationQueriesSchemes Array instagram

Write your image data to a file in tmp directory.

let imageData = UIImageJPEGRepresentation(YourImage, 1.0)
let temporaryDirectory = NSTemporaryDirectory() as NSString
let temporaryImagePath = temporaryDirectory.stringByAppendingPathComponent("YourImageFileName.igo")
let boolValue = imageData.writeToFile(temporaryImagePath, atomically: true)

Create an instance of the UIDocumentInteractionController class.

lazy private var documentInteractionController = UIDocumentInteractionController()

Set the URL property and the UTI property, and call the presentOpenInMenuFromRect method to display the menu.

documentInteractionController.URL = temporaryImagePath
documentInteractionController.UTI = "com.instagram.exclusivegram"
documentInteractionController.presentOpenInMenuFromRect(
    YourView.bounds,
    inView: YourView,
    animated: true
)

The rest is a operation on your iOS device. Tap the "Copy to Instagram" icon, and Instagram app is presented with its filter screen.

Introduction my library

EggsBenedict

github.com

Availability

This library has functions at the very least, but I'd like to announce right now not to make mistakes.

Reference

I've referenced the following articles. Thanks!

Related to Instagram

qiita.com

maplesystems.co.jp

Related to Carthage

www.slideshare.net

www.slideshare.net

qiita.com

qiita.com

realm.io

Related to CocoaPods

qiita.com

Others

I've used the following Web application for creating an animation GIF. Thanks!

hitode909.hatenablog.com