在swift

时间:2015-06-17 16:24:18

标签: ios swift twitter

我正在使用TwitterKit,我需要使用图片或视频以及描述发布状态。麻烦的是它没有给我任何类型的错误代码,所以我不知道它为什么不工作。这是我到目前为止的代码:

编辑:好的,我现在收到一个错误。错误是

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

编辑2:确定以下所有代码都正常工作,除非我尝试上传视频时发回400错误错误请求

所以一切都是一样的,除了我改变了代码的媒体行:

var imageData : NSData = UIImageJPEGRepresentation(self.image!, 0.5)
                parameters["media"] = imageData.base64EncodedStringWithOptions(nil)

到这个

parameters["media"] = self.video!.base64EncodedStringWithOptions(nil) //self.video is NSData

这是代码:

 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
                    }
                }

1 个答案:

答案 0 :(得分:0)

最新的Twitter API使得在Twitter上发布带有图像和消息的推文非常容易。根据您上面的示例,下面是一个适合我的示例代码。如果您遇到任何问题,请验证并告知我们。更多细节 - &gt; Twitter API's github page

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
                }
            }