Search code examples
iosswiftinstagraminstagram-api

Share video to Instagram feed from iOS


I've been trying to create a sharing experience for our app where Instagram launches giving these two options:

enter image description here

Facebook has a pretty lean documentation about it. I tried all the possible permutations using the UIDocumentInteractionController. I tried using as uti com.instagram.photo and com.instagram.video with the ig extension but I keep getting the standard sharing popover instead of launching Instagram directly. Tried also com.instagram.exclusivegram with igo but that seems to be supposed to trigger the standard popover anyway.

Latest code:

func shareVideo(_ filePath: String) {
  let url = URL(fileURLWithPath: filePath)
  if(hasInstagram()){
    let newURL = url.deletingPathExtension().appendingPathExtension("ig")
    do {
      try FileManager.default.moveItem(at: url, to: newURL)
    } catch { print(error) }

    let dic = UIDocumentInteractionController(url: newURL)
    dic.uti = "com.instagram.photo"
    dic.presentOpenInMenu(from: self.view.frame, in: self.view, animated: true)
  }
}

Solution

  • The only way to get to the screen illustrated above is to save first the video in the library and then use the undocumented hook instagram://library passing the asset localIdentifier. Don't forget to add instagram query scheme in the info.plist.

    if UIApplication.shared.canOpenURL("instagram://app") { // has Instagram
        let url = URL(string: "instagram://library?LocalIdentifier=" + videoLocalIdentifier)
    
        if UIApplication.shared.canOpenURL(url) {
            UIApplication.shared.open(url, options: [:], completionHandler:nil)
        }
    }