How can I use swift version of SLRequest

I try to use SLRequest to post both text and meida to twitter. But somehow only text can be posted. I barely find any information that how to use this class. What's more since twitter change its API in terms of posting media all informaiton I can find in the internet dosn't work anymore. I post the same question on twitter's forum but no body answer me. Is there anybody who can help me.




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:/

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)")

})

}

}

})

}

Thanks for your answer. I have looked at the two APIs you mention. But I am still not too sure how to wrap my request. I think addMultipartData needs to be used, but not too sure either. I have found a lot of objective-c code, they use addMultipartData to add media stuff and then send it with text.

Really appreciate. Thank you very much. You remind me how to send one post with two requests. There is just a small mistake in the above code which is in the first request "media_data" parameter is also required along with "media". After I add that paratmenter in dictionary it works perfectly. Again thank you very much.

I'm able to use one request as long as update_with_media.json is specified, like so:


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_with_media.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
}
})
}

This is great guys. I've been bashing my head-in trying to get a specific user ID's follower count to no avail. I'm using Twitter's Fabric Kit for login but apart from that nothing seems to work. Any advice? I know this is vague.

How can I use swift version of SLRequest
 
 
Q