swift2 使用类似于SLComposeViewController的Swift将用户界面图像发布到Instagram

j2cgzkjk  于 2022-11-06  发布在  Swift
关注(0)|答案(3)|浏览(194)

我正在开发一个iOS Xcode 7 Swift 2项目。该应用程序使用以下方式将照片发布到FacebookTwitter

var shareToFacebook: SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeFacebook)

var shareToTwitter: SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeTwitter)

我喜欢在这两个社交媒体上发布一张照片是多么的容易和简单。我不需要也不想要它们的API。
我想为Instagram做一些类似的简单的事情。在不需要处理Instagram API的情况下,做这件事的最好方法是什么?或者我别无选择?
我的照片data是使用NSCoding保存的,而不是在DocumentDirectory中。我寻找here进行集成,但这是针对保存在应用程序directory中的照片。
只是一些简单的东西,就像我已经为Facebook和Twitter做的那样。

lymgl2op

lymgl2op1#

针对Swift 3更新

import UIKit
import Foundation

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 UIImageJPEGRepresentation(imageInstagram, 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 {

            /// display some message about how it didn't work
            /// Like: UIAlertController(title: kAlertViewTitle, message: kAlertViewMessage, preferredStyle: .alert)
        }
    }
}

然后在您要调用它的ViewController中...

InstagramManager.sharedManager.postImageToInstagramWithCaption(imageInstagram: <YOUR IMAGE>, instagramCaption: shareText, view: self.view)

如果安装了Instagram,这将打开“open-in”功能

snvhrwxg

snvhrwxg2#

我查找了here并找到了一个解决方案:
我创建了一个NSObject

import UIKit
import Foundation

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.sharedApplication().canOpenURL(instagramURL!) {
            let jpgPath = (NSTemporaryDirectory() as NSString).stringByAppendingPathComponent(kfileNameExtension)
            UIImageJPEGRepresentation(imageInstagram, 1.0)!.writeToFile(jpgPath, atomically: true)
            let rect = CGRectMake(0,0,612,612)
            let fileURL = NSURL.fileURLWithPath(jpgPath)
            documentInteractionController.URL = fileURL
            documentInteractionController.delegate = self
            documentInteractionController.UTI = kUTI

            // adding caption for the image
            documentInteractionController.annotation = ["InstagramCaption": instagramCaption]
            documentInteractionController.presentOpenInMenuFromRect(rect, inView: view, animated: true)
        } else {

            // alert displayed when the instagram application is not available in the device
            UIAlertView(title: kAlertViewTitle, message: kAlertViewMessage, delegate:nil, cancelButtonTitle:"Ok").show()
        }
    }

}

然后在我的@IBAction中,我使用了:

let image = self.photoImageView.image
InstagramManager.sharedManager.postImageToInstagramWithCaption(image!, instagramCaption: "\(self.description)", view: self.view)

这将打开一个带有“open-in”样式的菜单,用户可以在Instagram(如果安装)中打开应用程序。

0wi1tuuw

0wi1tuuw3#

这是一个swift 4+可重用的类。使用Xcode 10.1。

1.创建swift空文件。
1.将下面的代码复制并粘贴到文件中。

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)
}}

并在ViewController类中使用此函数。

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

相关问题