在iOS Swift上发布到Instagram屏幕

原学程将引见在iOS Swift上宣布到Instagram屏幕的处置办法,这篇学程是从其余处所瞅到的,而后减了1些海外法式员的疑问与解问,愿望能对于您有所赞助,佳了,上面开端进修吧。

在iOS Swift上发布到Instagram屏幕 教程 第1张

成绩描写

我正在尽力让从我的运用法式到Instagram的分享变患上轻易。我想要的是转到上面屏幕截图上所描写的屏幕。我试过Instagram-Stories://分享深度链交,我曾经浏览了一切这些文档:https://developers.facebook.com/docs/instagram/sharing-to-stories/

但是,不管我做甚么,当url计划操纵触收时,它直交将图象分享到小说中。我在这里错过了甚么?

以下是我的代码摘录:

  if let image = image {
 guard let urlScheme = URL(string: "instagram-stories://share"),
  let imageData = image.pngData() else {
  return
 }

 if UIApplication.shared.canOpenURL(urlScheme) {
  let pasterboardItems = [["com.instagram.sharedSticker.backgroundImage": imageData]]
  let pasterboardOptions = [UIPasteboard.OptionsKey.expirationDate: Date().addingTimeInterval(六0*五)]

  UIPasteboard.general.setItems(pasterboardItems, options: pasterboardOptions)

  UIApplication.shared.open(urlScheme, options: [:], completionHandler: nil)
 }
}

推举谜底

您须要做的是应用以下URL翻开Instagram运用法式:
instagram://library?LocalIdentifier=并作为参数PHAsset.localIdentifier传播。
因为某种缘由,此接洽未在文档🤷‍♂️中的所有地位列出

但是为了吸收您的图象/望频的当地标记,您必需起首将图象/望频保留到用户的图片库。
是以,终究代码将以下所示

let videoFileUrl: URL = URL(fileURLWithPath: "path/to/my/video")!
var localId: String?
PHPhotoLibrary.shared().performChanges({
 let request = PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: videoFileUrl)
 localId = request?.placeholderForCreatedAsset?.localIdentifier
}, completionHandler: { success, error in
 // completion handler is called on an arbitrary thread
 // but since you (most likely) will perform some UI stuff
 // you better move everything to the main thread.
 DispatchQueue.main.async {
  guard error == nil else {
// handle error
return
  }
  guard let localId = localId else {
// highly unlikely that it'll be nil,
// but you should handle this error just in case
return
  }

  let url = URL(string: "instagram://library?LocalIdentifier=(localId)")!
  guard UIApplication.shared.canOpenURL(url) else {
// handle this error
return
  }
  UIApplication.shared.open(url, options: [:], completionHandler: nil)
 }
})

佳了闭于在iOS Swift上宣布到Instagram屏幕的学程便到这里便停止了,愿望趣模板源码网找到的这篇技巧文章能赞助到年夜野,更多技巧学程不妨在站内搜刮。