本文共 2685 字,大约阅读时间需要 8 分钟。
The Twitter framework's
Before posting a Tweet with an attached image, you should check
Once you know the current Twitter configuration, you can post the Tweet. You use the
Notes:
- (void)performTWRequestUpload
{
NSURL *url =
[NSURL URLWithString:
@"https://upload.twitter.com/1/statuses/update_with_media.json"];
// Create a POST request for the target endpoint
TWRequest *request =
[[TWRequest alloc] initWithURL:url
parameters:nil
requestMethod:TWRequestMethodPOST];
// self.accounts is an array of all available accounts;
// we use the first one for simplicity
[request setAccount:[self.accounts objectAtIndex:0]];
// The "larry.png" is an image that we have locally
UIImage *image = [UIImage imageNamed:@"larry.png"];
// Obtain NSData from the UIImage
NSData *imageData = UIImagePNGRepresentation(image);
// Add the data of the image with the
// correct parameter name, "media[]"
[request addMultiPartData:imageData
withName:@"media[]"
type:@"multipart/form-data"];
// NB: Our status must be passed as part of the multipart form data
NSString *status = @"just setting up my twttr #iOS5";
// Add the data of the status as parameter "status"
[request addMultiPartData:[status dataUsingEncoding:NSUTF8StringEncoding]
withName:@"status"
type:@"multipart/form-data"];
// Perform the request.
// Note that -[performRequestWithHandler] may be called on any thread,
// so you should explicitly dispatch any UI operations to the main thread
[request performRequestWithHandler:
^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSDictionary *dict =
(NSDictionary *)[NSJSONSerialization
JSONObjectWithData:responseData options:0 error:nil];
// Log the result
NSLog(@"%@", dict);
dispatch_async(dispatch_get_main_queue(), ^{
// perform an action that updates the UI...
});
}];
}
转载地址:http://xjeji.baihongyu.com/