从iOS将视频分享到Instagram feed

Nut*_*ing 6 ios instagram swift instagram-api

我一直在尝试为我们的应用程序创建共享体验,其中Instagram启动时提供了以下两个选项:

在此处输入图片说明

Facebook有一个非常精简的文档。我使用UIDocumentInteractionController尝试了所有可能的排列。我尝试使用作为uti com.instagram.photocom.instagram.videoig扩展,但我不断收到标准共享酥料饼的,而不是直接启动的Instagram。也尝试过com.instagram.exclusivegramigo但这似乎应该触发标准弹出窗口。

最新代码:

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)
  }
}
Run Code Online (Sandbox Code Playgroud)

Nut*_*ing 7

进入上图所示屏幕的唯一方法是首先将视频保存在库中,然后使用instagram://library传递资产的未记录挂钩localIdentifier。不要忘记instagraminfo.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)
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 酷,谢谢!我发现如果您将 url 更改为 `"instagram://library?AssetPath="`,它也可以工作。你有没有想过是否也可以传递标题字符串? (2认同)
  • @dobranoc 在这里也一样。他们还有其他优先事项,比如剥削孩子。 (2认同)

Sid*_*nna 5

尝试这个 :-

我目前正在通过以下方式共享我上次保存的视频:

    let fetchOptions = PHFetchOptions()
    fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
    let fetchResult = PHAsset.fetchAssets(with: .video, options: fetchOptions)
    if let lastAsset = fetchResult.firstObject {
        let localIdentifier = lastAsset.localIdentifier
        let u = "instagram://library?LocalIdentifier=" + localIdentifier
        let url = NSURL(string: u)!
        if UIApplication.shared.canOpenURL(url as URL) {
            UIApplication.shared.open(URL(string: u)!, options: [:], completionHandler: nil)
        } else {

            let urlStr = "https://itunes.apple.com/in/app/instagram/id389801252?mt=8"
            if #available(iOS 10.0, *) {
                UIApplication.shared.open(URL(string: urlStr)!, options: [:], completionHandler: nil)

            } else {
                UIApplication.shared.openURL(URL(string: urlStr)!)
            }
        }

    }
Run Code Online (Sandbox Code Playgroud)