func twitterSender(photoImported: UIImage) ->Void {
let account = ACAccountStore()
let accountType = account.accountTypeWithAccountTypeIdentifier(
ACAccountTypeIdentifierTwitter)
account.requestAccessToAccountsWithType(accountType, options: nil,
completion: {(success: Bool, error: NSError!) -> Void in
if success {
let arrayOfAccounts =
account.accountsWithAccountType(accountType)
if arrayOfAccounts.count > 0 {
let twitterAccount = arrayOfAccounts.last as! ACAccount
var message = Dictionary<String, AnyObject>()
message["status"] = "My app test 5"
let imageData = UIImageJPEGRepresentation(photoImported, 0.9)
let imageString = imageData.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.allZeros)
message["media_ids"] = imageString
let requestURL = NSURL(string:
"https://api.twitter.com/1.1/statuses/update.json")
let postRequest = SLRequest(forServiceType:
SLServiceTypeTwitter,
requestMethod: SLRequestMethod.POST,
URL: requestURL,
parameters: message)
postRequest.addMultipartData(imageData, withName: "oauth_*", type: "application/octet-stream", filename: "image.jpg")
postRequest.account = twitterAccount
postRequest.performRequestWithHandler({
(responseData: NSData!,
urlResponse: NSHTTPURLResponse!,
error: NSError!) -> Void in
if let err = error {
println("Error : \(err.localizedDescription)")
}
println("Twitter HTTP response \(urlResponse.statusCode)")
})
}
}
})
}
我的代码的问题是只能发布没有图像的文本。我搜索了很多,并尝试在twitter的网站上找到一些信息。但我仍然很困惑如何做到这一点。 Twitter曾经有一个名为POST status / update_with_media的API来完成我现在想做的工作。不幸的是,twitter丢弃了该API并使用了新API。所以我确实在我的网站上找到了一些类似的问题,但他们都使用了objective-c或者使用了旧的twitter API。没有什么对我有帮助。有很多研究耗费了我很多时间,看起来我需要使用addMultipartData来完成这项工作,但我不知道如何填充这些参数,或者这也许是一个错误的方向。
答案 0 :(得分:1)
func twitter(){
let account = ACAccountStore()
let accountType = account.accountTypeWithAccountTypeIdentifier(
ACAccountTypeIdentifierTwitter)
account.requestAccessToAccountsWithType(accountType, options: nil,completion: {(success: Bool, error: NSError!) -> Void in
if success {
let arrayOfAccounts = account.accountsWithAccountType(accountType)
if arrayOfAccounts.count > 0 {
let twitterAccount = arrayOfAccounts.last as! ACAccount
var message = Dictionary<String, AnyObject>()
message["status"] = self.txtPostDesc.text! //textbox
let imageData = UIImagePNGRepresentation(self.imagePost)//pickerview add
let imageString = imageData!.base64EncodedStringWithOptions(NSDataBase64EncodingOptions())
message["media_ids"] = imageString
let requestURL = NSURL(string: "https://upload.twitter.com/1/statuses/update_with_media.json")
let postRequest = SLRequest(forServiceType: SLServiceTypeTwitter, requestMethod: SLRequestMethod.POST, URL: requestURL, parameters: message)
postRequest.addMultipartData(imageData, withName: "media", type: nil, filename: nil)
postRequest.account = twitterAccount
postRequest.performRequestWithHandler({(responseData: NSData!,urlResponse: NSHTTPURLResponse!,error: NSError!) -> Void in
if let err = error {
print("Error : \(err.localizedDescription)")
}
print("Twitter HTTP response \(urlResponse.statusCode)")
self.alertShow("successful")
})
}
}
})
}
答案 1 :(得分:-1)
您需要使用update.json并使用NSData为addMultipartData提供图像。这对我有用:
func tweetWithImage(data:NSData)
{
let account = ACAccountStore()
let accountType = account.accountTypeWithAccountTypeIdentifier(
ACAccountTypeIdentifierTwitter)
account.requestAccessToAccountsWithType(accountType, options: nil,
completion: {(success: Bool, error: NSError!) -> Void in
if success {
let arrayOfAccounts =
account.accountsWithAccountType(accountType)
if arrayOfAccounts.count > 0 {
let twitterAccount = arrayOfAccounts.first as! ACAccount
var message = Dictionary<String, AnyObject>()
message["status"] = "Test Tweet with image"
let requestURL = NSURL(string:
"https://api.twitter.com/1.1/statuses/update.json")
let postRequest = SLRequest(forServiceType:
SLServiceTypeTwitter,
requestMethod: SLRequestMethod.POST,
URL: requestURL,
parameters: message)
postRequest.account = twitterAccount
postRequest.addMultipartData(data, withName: "media", type: nil, filename: nil)
postRequest.performRequestWithHandler({
(responseData: NSData!,
urlResponse: NSHTTPURLResponse!,
error: NSError!) -> Void in
if let err = error {
println("Error : \(err.localizedDescription)")
}
println("Twitter HTTP response \(urlResponse.statusCode)")
})
}
}
else
{
// do what you want here
}
})
}
我已经在我的博客上添加了一个教程,其中包含GitHub中的相应项目,该项目说明了使用SLRequest以便支持动画GIF ...您可以在此处看到它:http://www.iosinsight.com/twitter-integration-with-swift/
注意:此代码和博客帖子根据maml关于使用简单&#34;更新&#34;的评论进行更新。而不是弃用&#34; update_with_media&#34;。