问题描述
我有一个iOS Xcode 7 Swift 2项目我正在工作。应用程序将照片发送到 Facebook
和 Twitter
使用: var shareToFacebook:SLComposeViewController = SLComposeViewController(forServiceType:SLServiceTypeFacebook)
var shareToTwitter:SLComposeViewController = SLComposeViewController(forServiceType:SLServiceTypeTwitter)
爱上这两张社交媒体照片,感觉如何轻松简单。我不需要或想要他们的API。
我想为 Instagram
做类似和简单的事情。没有必要处理 Instagram API
的最佳方法是什么?或者我别无选择?
我的照片数据
使用 NSCoding
,而不在 DocumentDirectory
中。我查看了以进行集成,但这是为了保存一张照片在应用程序目录
。
只是一些简单的东西,就像我已经拥有的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 =请安装Instagram应用程序
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){
//调用以发布图像标题到instagram应用程序
let instagramURL = NSURL(string:kInstagramURL)
如果UIApplication.sharedApplication()。canOpenURL(instagramURL!){
let jpgPath =(NSTemporaryDirectory()作为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
//为图像添加标题
documentInteractionController.annotation = [InstagramCaption:instagramCaption]
documentInteractionController.presentOpenInMenuFromRect(rect,inView:view,animated:true)
} else {
//当instagram应用程序不是时显示警报在设备中可用
UIAlertView(title:kAlertViewTitle,message:kAlertViewMessage,delegate:nil,cancelButtonTitle:Ok)。show()
}
}
然后在我的 @IBAction
:
let image = self.photoImageView.image
InstagramManager.sharedManager.postImageToInstagramWithCaption(image !, instagramCaption:\\ \\(self.description),v iew:self.view)
这将打开一个带有开放风格的菜单,用户可以在 Instagram
(如果已安装)中打开该应用程序。
I have an iOS Xcode 7 Swift 2 project I'm working on. The app posts photos to Facebook
and Twitter
using:
var shareToFacebook: SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
and
var shareToTwitter: SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeTwitter)
Love how easy and simple for just posting a photo to these two social medias. I didn't need or want the APIs for them.
I want to do something similar and simple for Instagram
. What is the best way to do this without having to deal with Instagram API
? Or do I have no choice?
My photo data
is saved using NSCoding
, not in the DocumentDirectory
. I looked here for integration but this is for a photo saved in the app directory
.
Just something simple like what I already have for Facebook and Twitter.
I looked here and found a solution:
I created an 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()
}
}
}
Then in my @IBAction
I used:
let image = self.photoImageView.image
InstagramManager.sharedManager.postImageToInstagramWithCaption(image!, instagramCaption: "\(self.description)", view: self.view)
This opens bring a menu up with the 'open-in' style and the user can open the app in Instagram
(if installed).
这篇关于使用Swift类似于SLComposeViewController将Instagram发布到Instagram的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!