I have try to share video by twitter api
First my try was to share by with TWTRComposerViewController
-
I try to share url that was created like this:
let tempPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .allDomainsMask, true)
let tempDocumentsDirectory = tempPath[0] as AnyObject
let uniqueVideoID = "shareVideo.mp4"
let filePath = tempDocumentsDirectory.appendingPathComponent(uniqueVideoID) as String
deleteIfExist(filePath: filePath)
let url = URL(fileURLWithPath: filePath)
but had an error ‘NSInvalidArgumentException’, reason: 'Application tried to present a nil modal view controller on target
-
After that i was try to save video to Gallery and get url
let fetchOptions = PHFetchOptions()
fetchOptions.sortDescriptors = [NSSortDescriptor(key: “creationDate”, ascending: true)]
// After uploading we fetch the PHAsset for most recent video and then get its current location url
let fetchResult = PHAsset.fetchAssets(with: .video, options: fetchOptions)
if let first = fetchResult.lastObject {
PHImageManager().requestAVAsset(forVideo: first, options: nil, resultHandler: { (avurlAsset, audioMix, dict) in
DispatchQueue.main.async {
let newObj = avurlAsset as! AVURLAsset
let shareUrl = newObj.url
}
And still gets the same error
-
I was try to get url from camera roll
let mediaUI = UIImagePickerController()
mediaUI.sourceType = .savedPhotosAlbum
mediaUI.mediaTypes = UIImagePickerController.availableMediaTypes(for: .savedPhotosAlbum)!
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
picker.dismiss(animated: true, completion: {
let type = UIImagePickerController.availableMediaTypes(for: .savedPhotosAlbum)!
let url = info[UIImagePickerControllerMediaURL] as! NSURL
}
}
And still gets the same error
-
I was try to pick video from camera roll that was recorded by native twitter app and still gets the same error.
-
After that i was try to share video by INIT, APPEND, FINALIZE requests
func tweetVideoInit(videoData: Data) {
let params = [“command” : “INIT”, “media_type” : “video/mp4” , “total_bytes” : “(videoData.count)”]
let client = TWTRAPIClient.withCurrentUser()
var initError: NSError?
var postRequest = client.urlRequest(withMethod: self.POST, url: strUploadUrl, parameters: params, error: &initError)
client.sendTwitterRequest(postRequest, completion: { (_, responseData, error) in
if let err = error {
print(“tweetVideoInit”)
print(err.localizedDescription)
self.socialProtocol.errorAction(error: err.localizedDescription)
} else {
do {
let object = try JSONSerialization.jsonObject(with: responseData!, options: .allowFragments)
if let dictionary = object as? [String: AnyObject] {
if let tweetID = dictionary[“media_id_string”] as? String {
self.tweetVideoApped(videoData: videoData, videoSize: videoData.count, mediaId: tweetID, chunk: 0)
}
}
} catch {
self.socialProtocol.errorAction(error: error.localizedDescription)
print(error)
}
}
})
}
func tweetVideoApped(videoData: Data, videoSize: Int , mediaId: String, chunk: NSInteger) {
let video = videoData.base64EncodedString()
let params = [“command”: “APPEND”, “media_id”: mediaId, “media_data”: video, “segment_index”: “(chunk)” ]
let client = TWTRAPIClient.withCurrentUser()
var initError: NSError?
var postRequest = client.urlRequest(withMethod: self.POST, url: strUploadUrl, parameters: params, error: &initError)
client.sendTwitterRequest(postRequest, completion: { (_, responseData, error) in
print(responseData?.asData())
if let err = error {
self.socialProtocol.errorAction(error: err.localizedDescription)
print("tweetVideoApped")
print(err)
} else {
self.postStatus(mediaId: mediaId)
}
})
}
func postStatus(mediaId: String) {
var params = String: String
params[“command”] = "STATUS"
params[“media_id”] = mediaId
let client = TWTRAPIClient.withCurrentUser()
var initError: NSError?
let postRequest = client.urlRequest(withMethod: self.POST, url: strUploadUrl, parameters: params, error: &initError)
client.sendTwitterRequest(postRequest, completion: { (urlResponse, responseData, error) in
if let err = error {
self.socialProtocol.errorAction(error: err.localizedDescription)
print("postStatus")
print(err)
self.tweetVideoFinalize(mediaId: mediaId)
} else {
do {
let object = try JSONSerialization.jsonObject(with: responseData!, options: .allowFragments)
if let dictionary = object as? [String: AnyObject] {
print("video uploaded")
print(dictionary)
}
self.tweetVideoFinalize(mediaId: mediaId)
} catch {
print(error)
self.socialProtocol.errorAction(error: error.localizedDescription)
}
}
})
}
func tweetVideoFinalize(mediaId: String) {
var params = [String:String]()
params["command"] = "FINALIZE"
params["media_id"] = mediaId
let client = TWTRAPIClient.withCurrentUser()
var initError: NSError?
var postRequest = client.urlRequest(withMethod: self.POST, url: strUploadUrl, parameters: params, error: &initError)
client.sendTwitterRequest(postRequest, completion: { (_, responseData, error) in
if let err = error {
print(err)
self.socialProtocol.errorAction(error: err.localizedDescription)
} else {
do {
let object = try JSONSerialization.jsonObject(with: responseData!, options: .allowFragments)
if let _ = object as? [String: AnyObject] {
self.socialProtocol.succsessAction(message: "Succsess")
}
} catch {
self.socialProtocol.errorAction(error: error.localizedDescription)
print(error)
}
}
})
}
And always was get error NSLocalizedFailureReason=Twitter API error : media parameter is missing. (code 38)
If i added this parameter i gets Error Domain=TWTRNetworkingErrorDomain Code=-1011 "Request failed: bad request (400)"
My question is. Do you have working tutorial for sharing video for ios?