Share Extensionで共有ボタンが押された時アプリを表示する
はじめに
Safariなどで画像のボダンからデータの共有を行う際に共有先のアプリの選択を行うが、どうやって出すのか気になったので調べてみた。
今回表示するのは【共有オプション】への表示です。アクションオプションへの表示は別途調べようと思います。

Share Extensionファイルの追加
新規作成

ファイルの選択

名前をつけて作成

Activateを選択

ファイルの確認
作成した名前のShare Extensionフォルダが作成されていることを確認します。

ShareViewController
ShareViewControllerの中でデータを受け取った際の処理を記述するようです。
import UIKit
import Social
class ShareViewController: SLComposeServiceViewController {
override func isContentValid() -> Bool {
// Do validation of contentText and/or NSExtensionContext attachments here
return true
}
override func didSelectPost() {
// This is called after the user selects Post. Do the upload of contentText and/or NSExtensionContext attachments.
// Inform the host that we're done, so it un-blocks its UI. Note: Alternatively you could call super's -didSelectPost, which will similarly complete the extension context.
self.extensionContext!.completeRequest(returningItems: [], completionHandler: nil)
}
override func configurationItems() -> [Any]! {
// To add configuration options via table cells at the bottom of the sheet, return an array of SLComposeSheetConfigurationItem here.
return []
}
}
Capabilitiesの設定
Capabilitiesの中にあるApp GroupsとKeychain SharingをONにします


App GroupsにBundleIDを追記
➕ボタンからgroup.BundleIDを追記します。
これがなくても共有ボタンを押した際表示され値を受け取れますが、実際のアプリ側にデータを共有することができなくなります。

確認
Safariの画面で共有ボタンを押すとアプリが表示されます。


処理を送るとdidSelectPostメソッドが呼ばれます
override func didSelectPost() {
self.extensionContext!.completeRequest(returningItems: [], completionHandler: nil)
}