使用类似于SLComposeViewController的Swift将UIImage发布到Instagram

Cha*_*Guy 6 facebook ios instagram swift2

我正在开发一个iOS Xcode 7 Swift 2项目.该应用程序发布照片FacebookTwitter使用:

var shareToFacebook: SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
Run Code Online (Sandbox Code Playgroud)

var shareToTwitter: SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeTwitter)
Run Code Online (Sandbox Code Playgroud)

喜欢将照片发布到这两个社交媒体是多么容易和简单.我不需要或想要它们的API.

我想做类似和简单的事情Instagram.在不必处理的情况下,最好的方法是什么Instagram API?或者我别无选择?

我的照片data是使用保存的NSCoding,而不是保存在DocumentDirectory.我在这里寻找集成,但这是为了保存在应用程序中的照片directory.

就像我已经拥有的Facebook和Twitter一样简单.

小智 7

针对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)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

然后在ViewController中你要调用它...

InstagramManager.sharedManager.postImageToInstagramWithCaption(imageInstagram: <YOUR IMAGE>, instagramCaption: shareText, view: self.view)
Run Code Online (Sandbox Code Playgroud)

如果安装了Instagram,这将打开"开放"功能


Cha*_*Guy 6

我看着这里,发现一个解决方案:

我创建了一个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()
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

然后在我@IBAction使用的:

let image = self.photoImageView.image
InstagramManager.sharedManager.postImageToInstagramWithCaption(image!, instagramCaption: "\(self.description)", view: self.view)
Run Code Online (Sandbox Code Playgroud)

这将打开一个带有"open-in"样式的菜单,用户可以打开应用程序Instagram(如果已安装).

  • 通过这种方式,我只看到带有标签的警告表:"复制到Instagram"我必须点击它来分享.有没有直接的方式在应用程序内共享照片? (7认同)
  • 这是有效的,但对于ios9,您必须在plist文件中添加以下内容:<key> LSApplicationQueriesSchemes </ key> <array> <string> instagram </ string> </ array> (2认同)