3

I was trying to share the URL and text and Image on instagram from my IOS app. I was able to share the image but not text and URL. Then I read about instagram policy, they don't allow to share the text anymore according to this post. Here's a link! So now I have decided to share the text message and URL to instagram Direct. I already find URL scheme in Instagram dev doc, but there is no one word about sharing to direct. I also check some third part libraries - nothing helpful.

Here is the code I have tried to share .. But InstagramCaption doesn't work.

    class InstagramManager: NSObject, 
    UIDocumentInteractionControllerDelegate {
    private let kInstagramURL = "instagram://app"
    private let kUTI = "com.instagram.exclusivegram"
    private let kfileNameExtension = "instagram.igo"
    private let kAlertViewTitle = "Error"
    private let kAlertViewMessage = "Please install the Instagram 
      application"

    var documentInteractionController = 
      UIDocumentInteractionController()

    // singleton manager
    class var sharedManager: InstagramManager {
        struct Singleton {
            static let instance = InstagramManager()
        }
        return Singleton.instance
    }

    func postImageToInstagramWithCaption(imageInstagram: UIImage, 
        instagramCaption: String, view: UIView) {
        // called to post image with caption to the instagram 
         application

        let instagramURL = NSURL(string: kInstagramURL)

        if UIApplication.shared.canOpenURL(instagramURL! as URL) {

            let jpgPath = (NSTemporaryDirectory() as 
          NSString).appendingPathComponent(kfileNameExtension)

            do {
                try 
          imageInstagram.jpegData(compressionQuality:1.0)?.write(to:  
           URL(fileURLWithPath: jpgPath), options: .atomic)


            } catch {

                print(error)
            }

            let rect = CGRect(x: 0, y: 0, width: 612, height: 612)
            let fileURL = NSURL.fileURL(withPath: jpgPath)

            documentInteractionController.url = fileURL

            documentInteractionController.delegate = self

            documentInteractionController.uti = kUTI

            // adding caption for the image

            documentInteractionController.annotation = 
            ["InstagramCaption": instagramCaption]

            documentInteractionController.presentOpenInMenu(from: rect, 
            in: view, animated: true)
        } else {

            Utilities.showAlert(kAlertViewTitle, withMessage: 
            kAlertViewMessage)
        }
    }
     }

IF anything you can guide me with.. i-e to share Text+URL+Image in newsfeeds or In direct message

Mohsin
  • 263
  • 2
  • 19

0 Answers0