LoginSignup
2
2

More than 5 years have passed since last update.

Facebook Share Dialog で画像やビデオをシェア(iOS9, swift2.3) [2016年10月]

Last updated at Posted at 2016-10-21

Facebook SDK で画像やビデオをシェアで困った

Facebook SDK に『アセットのURL』を渡さないといけませんが、ALAsset は iOS 9 で廃止になり、iOS 8 から使用が推奨されている PHAsset では URL が取れなくて困りました。
こちらのとりあえずの解決方法をメモしておきます。
※デザインにこだわりがない場合は、UIActivityViewController を使うのが簡単です。

PHAsset から URL を生成

localIdentifier を使います。

some-code.swift

// asset の URL は、以下の書式
// assets-library://asset/asset.<type>?id=<localIdentifier(36文字)>&ext=<type>
// type は以下の識別子
// video : MOV
// jpg : JPG
// png : PNG

let asset:PHAsset = "<共有したいAsset。カメラロールなどに保存してあること>"
let identifier = asset.localIdentifier
let id = identifier.substringToIndex(identifier.startIndex.advancedBy(36))
let url = "assets-library://asset/asset.MOV?id=\(id)&ext=MOV"

info.plist に、[assets-library] を登録

登録しないと、assets-library でコケます。

info.plist
LSApplicationQueriesSchemes
 item(?) : assets-library

あとは普通にシェア

some-code.swift(つづき)

let video = FBSDKShareVideo()
let content = FBSDKShareVideoContent()

video.videoURL = NSURL(string: url) // 上で求めた asset URL
content.video = video

// dialog.mode を設定する必要があるかもしれないのでインスタンス化してますが、
// ナシでも動きました。
let dialog = FBSDKShareDialog()
dialog.delegate = nil
dialog.shareContent = content
dialog.fromViewController = self
dialog.show()

以上です。

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2