Forked from vincent-efwuhn/twitter_upload_media.m
Created
September 26, 2013 07:59
-
-
Save vc7/6711143 to your computer and use it in GitHub Desktop.
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
| NSString *boundary = @"cce6735153bf14e47e999e68bb183e70a1fa7fc89722fc1efdf03a917340"; | |
| // create request | |
| NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; | |
| [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData]; | |
| [request setHTTPShouldHandleCookies:NO]; | |
| [request setTimeoutInterval:30]; | |
| [request setHTTPMethod:@"POST"]; | |
| // set Content-Type in HTTP header | |
| NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary]; | |
| [request setValue:contentType forHTTPHeaderField: @"Content-Type"]; | |
| // post body | |
| NSMutableData *body = [NSMutableData data]; | |
| // add status param | |
| [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; | |
| [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"status\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; | |
| [body appendData:[[NSString stringWithFormat:@"%@\r\n", @"The status with image"] dataUsingEncoding:NSUTF8StringEncoding]]; | |
| // add image data | |
| NSData *imageData = UIImageJPEGRepresentation([UIImage imageNamed:@"tori.jpg"], 1.0); | |
| if (imageData) { | |
| [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; | |
| [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"media[]\"; filename=\"media.png\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; | |
| [body appendData:[[NSString stringWithFormat:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; | |
| [body appendData:imageData]; | |
| [body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; | |
| } | |
| [body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; | |
| // setting the body of the post to the reqeust | |
| [request setHTTPBody:body]; | |
| // set URL | |
| [request setURL:[NSURL URLWithString:@"https://api.twitter.com/1.1/statuses/update_with_media.json"]]; | |
| [[PFTwitterUtils twitter] signRequest:request]; | |
| AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request | |
| success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { | |
| NSLog(@"Oh Yes: %@",[JSON valueForKey:@"created_at"]); | |
| } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { | |
| NSLog(@"Oh No"); | |
| }]; | |
| [operation start]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment