[iOS] アプリ内で管理している画像を Instagram へ投稿する

2016.07.07

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

はじめに

iOS アプリから 画像共有サービス「Instagram」へ画像を投稿する方法を調査する機会がありましたので、その結果をご紹介します。

検証環境

  • OS X 10.11.5 (15F34)
  • Xcode 7.3.1 (7D1014)
  • iOS Deployment Target 8.0
  • iPhone 6 Plus, iOS 9.3.2
  • Instagram 8.3

実現したいこと

  • 自分のアプリ内で管理している画像を Instagram へ投稿したい

Instagram API を使用する

Instagram の API ドキュメントを確認してみましたが、画像の投稿を行うためのエンドポイントはありませんでした!

投稿済みの画像などは取得出来るようです。

Custom URL Scheme を使用する

Instagram アプリの Custom URL Scheme のドキュメントを確認してみましたが、URL Scheme を使用して画像の投稿を行うことはできないようです。

Instagram アプリがサポートする URL Scheme とパラメータの一覧は以下のとおりです。アプリの起動などは出来るようです。

URL 内容
instagram://app Instagram アプリを起動する
instagram://camera Instagram アプリを起動し、カメラを開く
instagram://media?id=<MEDIA_ID> Instagram アプリを起動し、指定の<MEDIA_ID>の投稿ページに移動
instagram://user?username=<USERNAME> Instagram アプリを起動し、指定の<USERNAME>のプロフィールページに移動
instagram://location?id=<LOCATION_ID> Instagram アプリを起動し、指定の<LOCATION_ID>のページに移動
instagram://tag?name=<TAG> Instagram アプリを起動し、指定の<TAG>の検索結果に移動

UIDocumentInteractionController を使用する

UIDocumentInteractionController はアプリ間でファイルを受け渡す機能を提供するクラスです。

このクラスを使うことで、Instagram へ画像を渡すことができました。画像の投稿自体は Instagram アプリ側で行うことになります。

実装例

// Instagram用の投稿画像を作成
let image = UIImage(named: "image")
let imageData = UIImageJPEGRepresentation(image!, 0.9)

// ファイルのURLを UIDocumentInteractionController に渡す必要があるので、適当な場所に一旦保存する
// 拡張子は .igo を指定
let fileURL = NSURL(fileURLWithPath: NSHomeDirectory()).URLByAppendingPathComponent("Documents/image.igo")
imageData?.writeToURL(fileURL, atomically: true)

// UIDocumentInteractionController を準備する
self.controller = UIDocumentInteractionController.init(URL: fileURL)

// 写真の共有先を Instagram のみにするために UTI を"com.instagram.exclusivegram" にする
self.controller.UTI = "com.instagram.exclusivegram"

// キャプション(コメント)を追加する機能は現在使えない!!!
// http://developers.instagram.com/post/125972775561/removing-pre-filled-captions-from-mobile-sharing
// self.controller.annotation = ["InstagramCaption": "My message"]

// メニューを表示する
if UIApplication.sharedApplication().canOpenURL(NSURL.init(string: "instagram://app")!) {
    self.controller.presentOpenInMenuFromRect(self.view.frame, inView: self.view, animated: true)
} else {
    print("instagram がインストールされてません!")
}

canOpenURL メソッドについて

iOS 9 で canOpenURL メソッドを使う場合は、canOpenURL メソッドで使用する URL scheme を Info.plist に登録しておく必要があります!

// Info.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>LSApplicationQueriesSchemes</key> // ← これ!
    <array>
        <string>instagram</string>
    </array>

     ...

</dict>
</plist>

動作確認

UIDocumentInteractionControllerpresentOpenInMenuFromRect:inView:animated: メソッドを呼ぶと以下のようなメニューが表示されます。

ios-app-to-instagram-1

メニュー上の Instagram を選択すると、Instagramアプリが開き、画像の編集のウィザードが始まります。

ios-app-to-instagram-2

あとは画面の指示に従って操作を行なって投稿を実行します。

まとめ

本記事では、iOS アプリから画像共有サービス「Instagram」へ画像を投稿する方法を調査した結果を紹介しました。

今回調べた限りでは、アプリ内で管理している画像を Instagram へ投稿する方法としては以下の方法しかないようです。

  • UIDocumentInteractionController を使用する
    1. UIDocumentInteractionController を使用して Instagram アプリへ画像を渡す
    2. 画像の投稿自体は Instagram アプリ側で行う

参考資料

Instagram 公式

その他