Post UIImage to Instagram using Swift similar to SLComposeViewController

前端 未结 3 638
执笔经年
执笔经年 2020-12-14 12:55

I have an iOS Xcode 7 Swift 2 project I\'m working on. The app posts photos to Facebook and Twitter using:

var shareToFacebook: SL         


        
3条回答
  •  萌比男神i
    2020-12-14 13:23

    Here is a reusable class with swift 4+. Working in Xcode 10.1.

    1. Make swift empty file.
    2. copy and paste the below code into the file.

      import UIKit
      import Foundation
      
      class InstagramHelper: NSObject, UIDocumentInteractionControllerDelegate {
      
          private let kInstagramURL = "instagram://den3079"
          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: InstagramHelper {
              struct Singleton {
                  static let instance = InstagramHelper()
              }
              return Singleton.instance
          }
      
      
          func postImageToInstagramWithCaption(imageInstagram: UIImage, instagramCaption: String, controller: UIViewController) {
              // called to post image with caption to the instagram application
      
              let instagramURL = URL(string: kInstagramURL)
      
              DispatchQueue.main.async {
      
                  if UIApplication.shared.canOpenURL(instagramURL!) {
      
                      let jpgPath = (NSTemporaryDirectory() as NSString).appendingPathComponent(self.kfileNameExtension)
                      if let jpegData = UIImageJPEGRepresentation(imageInstagram, 1.0) {
                          do {
                              try jpegData.write(to: URL(fileURLWithPath: jpgPath), options: .atomicWrite)
      
                          } catch {
                              print("write image failed")
                          }
                      }else {
                          let jpegData = UIImagePNGRepresentation(imageInstagram)
                          do {
                              try jpegData?.write(to: URL(fileURLWithPath: jpgPath), options: .atomicWrite)
      
                          } catch {
                              print("write image failed")
                          }
                      }
                      let fileURL = NSURL.fileURL(withPath: jpgPath)
                      self.documentInteractionController.url = fileURL
                      self.documentInteractionController.delegate = self
                      self.documentInteractionController.uti = self.kUTI
      
                      // adding caption for the image
                      self.documentInteractionController.annotation = ["InstagramCaption": instagramCaption]
                      self.documentInteractionController.presentOpenInMenu(from: controller.view.frame, in: controller.view, animated: true)
      
                  } else {
      
                      // alert displayed when the instagram application is not available in the device
                      self.showAlert("", message: self.kAlertViewMessage, controller: controller)
                  }
              }
          }
       func showAlert(_ title: String, message: String, controller : UIViewController){
      
          let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
          let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: { action -> Void in
              //Just dismiss the action sheet
              let selector: Selector = NSSelectorFromString("alertOkButtonHandler")
              if controller.responds(to: selector){
                  _ = controller.perform(selector)
              }
          })
          alert.addAction(okAction)
          controller.present(alert, animated: true, completion: nil)
      }}
      

    And use of this function in ViewController class.

    InstagramHelper.sharedManager.postImageToInstagramWithCaption(imageInstagram: img!, instagramCaption: "www.google.com", controller: self)
    

提交回复
热议问题