【问题标题】:How to share image in instagram?Swift如何在 Instagram 中分享图片?Swift
【发布时间】:2015-07-10 09:38:47
【问题描述】:

对不起,没有代码的问题。但我没有找到任何地方可以寻找。 我想在 Instagram 中分享带有标题的图片?我该怎么做?

任何帮助都会很棒

【问题讨论】:

  • 除非发生了我不知道的更改,否则您无法将图像发布到 instgram api。您可能想尝试更通用的共享选项:nshipster.com/uiactivityviewcontroller
  • 您想用您的应用程序提供的图像打开 Instagram 应用程序吗?或者您想代表您的应用程序将图像上传到用户 Instagram?
  • @Logan 谢谢..我会调查那个..
  • @DanielStorm 我想做你提到的第二个..我该怎么做?
  • @copeME 你可以用 OAuth Swift 做 DanielStorm 提到的事情。 OAuth 是您可以对个人帐户进行任何操作(上传、关注等)的唯一方法,下面提到的第三方库会为您处理所有这些。我强烈推荐使用它。

标签: ios swift instagram


【解决方案1】:

如果你不想使用UIDocumentInteractionController

SWIFT 5 更新

import Photos
...

func postImageToInstagram(image: UIImage) {
    UIImageWriteToSavedPhotosAlbum(image, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil)
}
@objc func image(_ image: UIImage, didFinishSavingWithError error: Error?, contextInfo: UnsafeRawPointer) {
        if error != nil {
            print(error)
        }

        let fetchOptions = PHFetchOptions()
        fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]

        let fetchResult = PHAsset.fetchAssets(with: .image, options: fetchOptions)

        if let lastAsset = fetchResult.firstObject as? PHAsset {

            let url = URL(string: "instagram://library?LocalIdentifier=\(lastAsset.localIdentifier)")!

            if UIApplication.shared.canOpenURL(url) {
                UIApplication.shared.open(url)
            } else {
                let alertController = UIAlertController(title: "Error", message: "Instagram is not installed", preferredStyle: .alert)
                alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
                self.present(alertController, animated: true, completion: nil)
            }

        }
}

【讨论】:

  • 兄弟.. 我一直在寻找。不将图片保存到照片库也可以这样做吗?
  • 我只是想知道为什么 instagram://library 不在 Instagram 文档中。无论如何,它对我有用。谢谢!
  • 谢谢!正是我想要的。尽管必须知道,他要求您的应用程序具有对照片库的写入权限。也许对某些人来说是一个障碍。
  • 如何分享视频?
【解决方案2】:
    class viewController: UIViewController, UIDocumentInteractionControllerDelegate {

    var yourImage: UIImage?
    var documentController: UIDocumentInteractionController!

    func shareToInstagram() {

     let instagramURL = NSURL(string: "instagram://app")

            if (UIApplication.sharedApplication().canOpenURL(instagramURL!)) {

                let imageData = UIImageJPEGRepresentation(yourImage!, 100)

                let captionString = "caption"

           let writePath = (NSTemporaryDirectory() as NSString).stringByAppendingPathComponent("instagram.igo")
           if imageData?.writeToFile(writePath, atomically: true) == false {

                    return

                } else {
   let fileURL = NSURL(fileURLWithPath: writePath)

                    self.documentController = UIDocumentInteractionController(URL: fileURL)

                    self.documentController.delegate = self

                    self.documentController.UTI = "com.instagram.exlusivegram"

                    self.documentController.annotation = NSDictionary(object: captionString, forKey: "InstagramCaption")
                          self.documentController.presentOpenInMenuFromRect(self.view.frame, inView: self.view, animated: true)

                }

            } else {
                print(" Instagram isn't installed ")
            }
        }
     }

    }

现在这仍然不适用于 iOS 9,因此您必须转到您的应用程序 info.plist,添加“LSApplicationQueriesSchemes”类型:数组,并在本例中添加 URL 方案“instagram”。

【讨论】:

  • 要完成这项工作,需要在 plist 中添加以下内容: LSApplicationQueriesSchemesinstagram
  • 我在 iOS 10.1 swift 3 中得到 Error Domain=NSCocoaErrorDomain Code=518 "The file couldn’t be saved because the specified URL type isn’t supported." UserInfo={NSURL=/private/var/mobile/Containers/Data/Application/5B80A983-5571-44A5-80D7-6A7B065800B5/tmp/instagram.igo} try imageData.write(to: writePath)
【解决方案3】:

Swift 3.0 版本:

 @IBAction func shareInstagram(_ sender: Any) {

        DispatchQueue.main.async {

            //Share To Instagram:
            let instagramURL = URL(string: "instagram://app")
            if UIApplication.shared.canOpenURL(instagramURL!) {

                let imageData = UIImageJPEGRepresentation(image, 100)
                let writePath = (NSTemporaryDirectory() as NSString).appendingPathComponent("instagram.igo")

                do {
                    try imageData?.write(to: URL(fileURLWithPath: writePath), options: .atomic)
                } catch {
                    print(error)
                }

                let fileURL = URL(fileURLWithPath: writePath)
                self.documentController = UIDocumentInteractionController(url: fileURL)
                self.documentController.delegate = self
                self.documentController.uti = "com.instagram.exlusivegram"

                if UIDevice.current.userInterfaceIdiom == .phone {
                    self.documentController.presentOpenInMenu(from: self.view.bounds, in: self.view, animated: true)
                } else {
                    self.documentController.presentOpenInMenu(from: self.IGBarButton, animated: true)
                }
            } else {
                print(" Instagram is not installed ")
            }
        }
    }

【讨论】:

  • 这个方法好像不行了,见例子repo
【解决方案4】:

这里试试这段代码

    @IBAction func shareContent(sender: UIButton) {

              let image = UIImage(named: "imageName")
            let objectsToShare: [AnyObject] = [ image! ]
            let activityViewController = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
            activityViewController.popoverPresentationController?.sourceView = self.view 


            activityViewController.excludedActivityTypes = [ UIActivityTypeAirDrop, UIActivityTypePostToFacebook ]


            self.presentViewController(activityViewController, animated: true, completion: nil)

            }
        }

【讨论】:

  • 是的,这正是您在现代 iOS 中分享到 Instagram(或其他任何地方)的方式。您永远不会尝试“从应用内部分享”。
  • 请注意,这在 iOS 13.1 中已损坏,我认为这不是您的代码,我认为 Apple 损坏了部分或全部共享扩展
猜你喜欢
  • 1970-01-01
  • 2017-10-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多