14

I have used following code for share image on WhatsApp but I am unable to set the caption text using following code.

I have tried annotation property of UIDocumentInteractionController,But in WhatsApp developer form there is not any key is specified for annotation.

I do know that we can do it by using UIImageGraphicContext, But I need to share URL as caption

if ([[UIApplication sharedApplication] canOpenURL: [NSURL URLWithString:@"whatsapp://app"]])
{
    NSString * savePath  = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/whatsAppTmp.wai"];

    [UIImageJPEGRepresentation([UIImage imageNamed:@"Convenor- image-SURANA1.png"], 1.0) writeToFile:savePath atomically:YES];

    _documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:savePath]];
    _documentInteractionController.annotation = [NSDictionary dictionaryWithObject:@"wwww.google.com" forKey:@"whatsappCaption"];

    _documentInteractionController.UTI = @"net.whatsapp.image";
    _documentInteractionController.delegate = self;

    [_documentInteractionController presentOpenInMenuFromRect:CGRectMake(0, 0, 0, 0) inView:self.view animated: YES];

} else {
    UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Alert." message:@"Por favor, instale Whatsapp." delegate:nil cancelButtonTitle:@"Está bem" otherButtonTitles:nil];
    [alert show];
}
Larme
  • 24,190
  • 6
  • 51
  • 81
Bhavik
  • 141
  • 1
  • 3
  • I don't see anything obvious in the [WhatsApp FAQ for iOS developers](http://www.whatsapp.com/faq/en/iphone/23559013). You might need to [email their product support at support@whatsapp.com](http://www.whatsapp.com/contact/). – Michael Dautermann Nov 23 '14 at 12:54
  • Thanks,I have already mailed to support but couldn't got their response. finally I did a trick I have shared an image url with text SO image will be available at that image url path – Bhavik Nov 26 '14 at 19:20

2 Answers2

4

You cannot pre-fill caption while sharing image on whatsapp. Facebook has already deprecated this functionality from Facebook, Instagram also.

You can either send a text or an image. But programmatically pre-filling caption is not possible.

The code in the question is fine to send an image. In order to send a plain text you can use the reference of below mentioned link:

http://www.whatsapp.com/faq/en/iphone/23559013

NOTE: Setting caption programmatically is not possible.

Hope this helps!

enter image description here

Dharmesh Siddhpura
  • 1,610
  • 12
  • 22
  • Facebook and twitter allow to publish url. They fetch image and title from the url. How do you do it with whatsapp? – Luda Sep 24 '15 at 14:10
  • You are right about Fb and Twitter. But coming to the pre-filled text/caption, Facebook, Instagram, WhatsApp does not allows that. I have uploaded a screenshot for whatsapp image+caption sharing. – Dharmesh Siddhpura Sep 24 '15 at 14:24
  • And if you just want to send a link then you can do it by using the url in my answer. But make sure that in place of that shared url, image and title in that url will not auto-populated. In whatsapp, shared url will only appear as a hyperlink. Hope this answered your question. – Dharmesh Siddhpura Sep 24 '15 at 14:36
  • I was looking for a more Facebook/Twitter functionality. But thanks – Luda Sep 29 '15 at 11:17
  • Above question of yours in comments section states that you want it for whatsapp because Facebook and twitter allows that. – Dharmesh Siddhpura Sep 29 '15 at 11:20
  • I wanted to achieve in Whatapp what can be achieved in Facebook and Twitter – Luda Sep 29 '15 at 11:22
  • Okay.. Thanks. And please do let me know if you find anything about it. – Dharmesh Siddhpura Sep 29 '15 at 11:24
1
NSError *error       = nil;
NSURL   *documentURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:&error];

UIImage *image     = [UIImage imageNamed:@"share.png"];
NSURL   *tempFile  = [documentURL URLByAppendingPathComponent:@"whatsAppTmp.wai"];
NSData  *imageData = UIImageJPEGRepresentation(image, 1.0);

NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

NSLog(@"%@",docDir);
NSLog(@"saving png");

NSString *pngFilePath = [NSString stringWithFormat:@"%@/test.png",docDir];
NSData *data1 = [NSData dataWithData:UIImageJPEGRepresentation(image,1.0)];
[data1 writeToFile:pngFilePath atomically:YES];
_documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:pngFilePath]];
_documentInteractionController.delegate = self;
_documentInteractionController.UTI = @"net.whatsapp.image";

[_documentInteractionController presentOpenInMenuFromRect:CGRectMake(0, 0, 0, 0) inView:self.view animated: YES];
Pang
  • 9,564
  • 146
  • 81
  • 122