I haven't found relevant and up to date answer on SO.
Here is the code I'm currently using, all requests are handled correctly but no video is posted ?
if let userID = Twitter.sharedInstance().sessionStore.session()?.userID {
var client = TWTRAPIClient(userID: userID)
let text: String = "Testing Video"
let videoLength: String = "\(self.video.length)"
print(videoLength)
var initError: NSError?
var message = ["status": text, "command" : "INIT", "media_type" : "video/m4v", "total_bytes" : videoLength]
let preparedRequest: NSURLRequest = client.URLRequestWithMethod("POST", URL: self.strUploadUrl, parameters: message, error: &initError)
client.sendTwitterRequest(preparedRequest, completion: { (urlResponse: NSURLResponse?, responseData: NSData?, error: NSError?) -> Void in
if error == nil {
do {
let json: NSDictionary = try (NSJSONSerialization.JSONObjectWithData(responseData!, options: NSJSONReadingOptions(rawValue: 0)) as? NSDictionary)!
print("JSON is \(json)")
let mediaID = json.objectForKey("media_id_string") as! String
client = TWTRAPIClient(userID: userID)
var uploadError: NSError?
let videoString = self.video.base64EncodedStringWithOptions([])
message = ["command" : "APPEND", "media_id" : mediaID, "segment_index" : "0", "media" : videoString]
let preparedRequest = client.URLRequestWithMethod("POST", URL: self.strUploadUrl, parameters: message, error: &uploadError)
client.sendTwitterRequest(preparedRequest, completion: { (urlResponse: NSURLResponse?, responseData: NSData?, error: NSError?) -> Void in
if error == nil {
client = TWTRAPIClient(userID: userID)
var finalizeError: NSError?
message = ["command":"FINALIZE", "media_id": mediaID]
let preparedRequest = client.URLRequestWithMethod("POST", URL: self.strUploadUrl, parameters: message, error: &finalizeError)
client.sendTwitterRequest(preparedRequest, completion: { (urlResponse: NSURLResponse?, responseData: NSData?, error: NSError?) -> Void in
if error == nil {
client = TWTRAPIClient(userID: userID)
var sendError: NSError?
let message = ["status": text, "wrap_links": "true", "media_ids": mediaID]
//var updateMessage = NSMutableDictionary(dictionary: message)
let preparedRequest = client.URLRequestWithMethod("POST", URL: self.strStatusUrl, parameters: message , error: &sendError)
client.sendTwitterRequest(preparedRequest, completion: { (urlResponse: NSURLResponse?, responseData: NSData?, error: NSError?) -> Void in
})
} else {
print("Command FINALIZE failed \n \(error!)")
}
})
} else {
print("Command APPEND failed")
}
})
}
catch {
print("\(error)")
}
}
else {
print("\(error.debugDescription)Command INIT failed")
}
})
}
All the code above is working, except that the video is not uploaded. I can't figure out what I'm missing and Twitter's documentation is very poor on posting video.