Post image with FHStwitterEngine it return error But if i post tweet without imahe than it post successfully
-(void)postTweet:(NSString*)message withImage:(UIImage*)img {
dispatch_async(GCDBackgroundThread, ^{
@autoreleasepool {
NSString *strMsg =[NSString stringWithFormat:@"%@",message];
NSData *imgData =UIImagePNGRepresentation(img);
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
NSError *returnCode = [[FHSTwitterEngine sharedEngine]postTweet:strMsg withImageData:imgData];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
NSString *title = nil;
NSString *message = nil;
if (returnCode) {
title = [NSString stringWithFormat:@"Error %d",returnCode.code];
message = returnCode.localizedDescription;
} else {
title = @"Tweet Posted";
message = message;
}
dispatch_sync(GCDMainThread, ^{
@autoreleasepool {
UIAlertView *av = [[UIAlertView alloc]initWithTitle:title message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[av show];
}
});
}
});
}
- (IBAction)btnPost:(id)sender {
NSString *str =@"Test5";
UIImage *image =[UIImage imageNamed:@"contact.png"];
if (image ==nil) {
[self postTweet:str];
}else {
[self postTweet:str withImage:image];
}
}
There is an issue with the current version of FHSTwitterEngine and posting images. I had the same issue in my app, but was able to solve it using the updates from this forked version:
https://github.com/alvani/FHSTwitterEngine/blob/master/FHSTwitterEngine/FHSTwitterEngine.m
It's easiest to just copy/paste that into FHSTwitterEngine.m, but here's the specific changes you'll need:
On or around Line 1599:
- (NSError *)sendPOSTRequestForURL:(NSURL *)url andParams:(NSDictionary *)params
...
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n",key] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:data];
Add the following code below:
if ([obj isKindOfClass:[NSData class]]) {
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
}
On or around Line 688:
- (NSError *)postTweet:(NSString *)tweetString withImageData:(NSData *)theData inReplyTo:(NSString *)irt {
...
NSMutableDictionary *params = [NSMutableDictionary dictionary];
params[@"media[]"] = theData;
Add the following code below:
params[@"status"] = tweetString;
Hope this helps!