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)")
})
}
}
})
}
The problem of my code is that only text can be posted without image. I search a lot and try to find some information on twitter's website. But I am still really confused how to do it. Twitter used to have a API called POST statuses/update_with_media to do the job I want to do now. Unfortunately twitter discarded that API and uses a new one. So I indeed found some similar questions with mine, but all of them either uses objective-c or uses that old twitter API. Nothing is helpful for me. With a lot of research consuming me a lot of time it looks like I need to use addMultipartData to do the job, but I don't know how to fill these parameters or maybe this is a wrong direction either.