Post image / video to twitter in swift

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://upload.twitter.com/1.1/media/upload.json, NSErrorFailingURLKey=https://upload.twitter.com/1.1/media/upload.json, NSLocalizedDescription=The request timed out., NSUnderlyingError=0x14611520 "The request timed out."})

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) //self.video is NSData

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>()
                        // get image from bundle
                        var imageData : NSData = UIImagePNGRepresentation(self.image!)

                        parameters["media"] = imageData.base64EncodedStringWithOptions(nil)
    //for uploading video would I just do this parameters["media'] = NSData.dataWithContentsOfMappedFile(url.relativePath!) as? NSData
                        var twUploadRequest = twAPIClient.URLRequestWithMethod("POST", URL: strUploadUrl, parameters: parameters, error: &error)
                        if true {
                            twAPIClient.sendTwitterRequest(twUploadRequest) {
                                (uploadResponse, uploadResultData, uploadConnectionError) -> Void in
                                if (uploadConnectionError == nil) {
                                    // using SwiftyJSON to parse result
                                    let json = JSON(data: uploadResultData!)
                                    // check for media id in result
                                    if (json["media_id_string"].string != nil) {
                                        println("result = \(json)")
                                        // post a status with link to media
                                        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 //(twStatusRequest != nil)
                                        {
                                            twAPIClient.sendTwitterRequest(twStatusRequest) { (statusResponse, statusData, statusConnectionError) -> Void in
                                                if (statusConnectionError != nil) {
                                                    println("Error posting status \(statusConnectionError)")
                                                }
                                            } // completion
                                        } else {
                                            println("Error creating status request \(error)")
                                        }
                                    } else {
                                        println("Media_id not found in result = \(json)")
                                    }
                                } else {
                                    println("Error uploading image \(uploadConnectionError)")
                                }
                            } // completion
                        } else {
                            println("Error creating upload request \(error)")
                        }
                        UIApplication.sharedApplication().networkActivityIndicatorVisible = false
                    }
                }
+4
source share
1 answer

API Twitter Twitter. , , , . , , - . → API- Twitter API

TWTRTwitter.sharedInstance().logInWithCompletion {
                (session, error) -> Void in
                if session != nil {
                    println("signed in as \(session.userName)")

                    let twitterUserID = TWTRTwitter.sharedInstance().sessionStore.session()?.userIDUIApplication.sharedApplication().networkActivityIndicatorVisible = true
                    var twAPIClient = TWTRAPIClient(userID: twitterUserID)
                    twitterAPIclient.sendTweet(withText: "Hey look at this", image: self.image!, completion: {(tweet, error) in

        if tweet?.createdAt != nil {
            print("Tweet posted: \(tweet.debugDescription)")
        }
        if error?.localizedDescription != nil {
            print("error posting Tweet: \(error?.localizedDescription ?? "default error message")")
        }
    })     UIApplication.sharedApplication().networkActivityIndicatorVisible = false
                }
            }
0

All Articles