Created
October 30, 2015 05:36
-
-
Save vikdenic/570060d52fb254a04c98 to your computer and use it in GitHub Desktop.
Twitter post with media
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| if shouldShareToTwitter == true { | |
| if didSaveToTwitter == false { | |
| //In ReviewInfoViewController, we have started a session with logInWithCompletion() | |
| let store = Twitter.sharedInstance().sessionStore | |
| if let userid = store.session()?.userID { | |
| let client = TWTRAPIClient(userID: userid) //we have this from logInWithCompletion() in the previousVC | |
| let imageData = UIImagePNGRepresentation(self.cardView.takeSnapshot()) | |
| let uploadParams = ["media" : imageData!.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.Encoding64CharacterLineLength)] | |
| //TODO: Handle error properly (do / catch?) | |
| let uploadRequest = client.URLRequestWithMethod(kTwitterPOSTmethod, URL: kTwitterUploadURL, parameters: uploadParams, error: nil) | |
| //First we upload the image via a request | |
| client.sendTwitterRequest(uploadRequest, completion: { (response, uploadResultData, error) -> Void in | |
| do { | |
| //Then we parse the resulting upload for the image | |
| let parsedObject = try NSJSONSerialization.JSONObjectWithData(uploadResultData!, options: NSJSONReadingOptions.AllowFragments) | |
| if let json = parsedObject as? NSDictionary { | |
| let media_id = json["media_id_string"] as! String | |
| let updateParams = ["status" : shareText, "media_ids" : media_id] | |
| //TODO: Handle error properly (do / catch?) | |
| let updateRequest = client.URLRequestWithMethod(kTwitterPOSTmethod, URL: kTwitterUpdateURL, parameters: updateParams, error: nil) | |
| //Lastly, we sent the status update along with the image that we parsed from the earlier request | |
| client.sendTwitterRequest(updateRequest, completion: { (response, data, connectionError) -> Void in | |
| if connectionError == nil { | |
| print("sendTwitterRequest(): \(response)") | |
| } | |
| else { | |
| print("sendTwitterRequest(): \(connectionError)") | |
| } | |
| }) | |
| } | |
| } | |
| catch { | |
| //TODO: Best way to handle error? | |
| print(error) | |
| } | |
| }) | |
| } | |
| didSaveToTwitter = true | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment