UIDocumentInteractionControllerでファイル送信
アプリ内に保持している画像を写真アプリに送信するサンプルを作ってみました。
UIDocumentInteractionControllerを使えばサクッと実装することができます。
コード
class ViewController: UIViewController {
var controller: UIDocumentInteractionController! = nil
override func viewDidLoad() {
super.viewDidLoad()
let img = UIImage(named: "img")!
let data = img.pngData()
let url = NSURL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("tmp.png")
do {
try data?.write(to: url!)
controller = UIDocumentInteractionController.init(url: url!)
} catch {
print("error")
}
if !controller!.presentOpenInMenu(from: view.frame, in: view, animated: true) {
print("対応するアプリがありません")
}
}
}
確認
アプリをビルドして確認してみます。
ビルドを行うと以下のViewが表示されるので共有アルバムを選択します


写真アプリを確認してみると保存できているのがわかります
