我有一个正在处理的iOS Xcode 7 Swift 2项目。该应用程序将照片发布到Facebook
并Twitter
使用:
var shareToFacebook: SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
和
var shareToTwitter: SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeTwitter)
喜欢将照片发布到这两个社交媒体上是多么容易和简单。我不需要或不需要它们的API。
我想为做些类似和简单的事情Instagram
。无需处理的最佳方法是什么Instagram API
?还是我别无选择?
我的照片data
是使用NSCoding
而不是保存的DocumentDirectory
。我在这里查看了集成,但这是保存在应用程序中的照片directory
。
就像我在Facebook和Twitter上已经拥有的一样简单。
我在这里查看并找到了解决方案:
我创建了一个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
(如果已安装)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句