I use TwitterKit, and I need to send the status with the image or video along with the description. The problem is that this does not give me any error code, so I do not know why it does not work. Here is the code that I still have:
EDIT: Now I get an error. Error
Error uploading image Optional(Error Domain=NSURLErrorDomain Code=-1001 "The request timed out." UserInfo=0x14604e90 {NSErrorFailingURLStringKey=https:
EDIT 2: OK, all of the codes below work, except when I try to download the video that it sends back. Error 400 errors
so everything is the same, except that I changed the media line of code:
var imageData : NSData = UIImageJPEGRepresentation(self.image!, 0.5)
parameters["media"] = imageData.base64EncodedStringWithOptions(nil)
to that
parameters["media"] = self.video!.base64EncodedStringWithOptions(nil)
And this is the code:
Twitter.sharedInstance().logInWithCompletion {
(session, error) -> Void in
if session != nil {
println("signed in as \(session.userName)")
let strUploadUrl = "https://upload.twitter.com/1.1/media/upload.json"
let strStatusUrl = "https://api.twitter.com/1.1/statuses/update.json"
UIApplication.sharedApplication().networkActivityIndicatorVisible = true
var twAPIClient = Twitter.sharedInstance().APIClient
var error: NSError?
var parameters:Dictionary = Dictionary<String, String>()
var imageData : NSData = UIImagePNGRepresentation(self.image!)
parameters["media"] = imageData.base64EncodedStringWithOptions(nil)
var twUploadRequest = twAPIClient.URLRequestWithMethod("POST", URL: strUploadUrl, parameters: parameters, error: &error)
if true {
twAPIClient.sendTwitterRequest(twUploadRequest) {
(uploadResponse, uploadResultData, uploadConnectionError) -> Void in
if (uploadConnectionError == nil) {
let json = JSON(data: uploadResultData!)
if (json["media_id_string"].string != nil) {
println("result = \(json)")
parameters = Dictionary<String, String>()
parameters["status"] = "Hey look at this"
parameters["media_ids"] = json["media_id_string"].string!
var twStatusRequest = twAPIClient.URLRequestWithMethod("POST", URL: strStatusUrl, parameters: parameters, error: &error)
if true
{
twAPIClient.sendTwitterRequest(twStatusRequest) { (statusResponse, statusData, statusConnectionError) -> Void in
if (statusConnectionError != nil) {
println("Error posting status \(statusConnectionError)")
}
}
} else {
println("Error creating status request \(error)")
}
} else {
println("Media_id not found in result = \(json)")
}
} else {
println("Error uploading image \(uploadConnectionError)")
}
}
} else {
println("Error creating upload request \(error)")
}
UIApplication.sharedApplication().networkActivityIndicatorVisible = false
}
}
Tyler source
share