2

If sending text to WhatsApp:

NSString *msg = @"Some Text";
NSString *urlWhats = [NSString stringWithFormat:@"whatsapp://send?text=%@",msg];
NSURL *whatsappURL = [NSURL URLWithString:[urlWhats stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
if ([[UIApplication sharedApplication] canOpenURL: whatsappURL]) {
  [[UIApplication sharedApplication] openURL: whatsappURL];
} else {
  UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"WhatsApp not installed." message:@"Your device has no WhatsApp installed." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
  [alert show];
}

If sending image to WhatsApp:

if ([[UIApplication sharedApplication] canOpenURL: [NSURL URLWithString:@"whatsapp://app"]]){

   UIImage* iconImage = [self captureScreen];// this will return a image
   NSString* savePath  = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/whatsAppTmp.wai"];

   [UIImageJPEGRepresentation(iconImage, 1.0) writeToFile:savePath atomically:YES];

   _documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:savePath]];
   _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:@"WhatsApp not installed." message:@"Your device has no WhatsApp installed." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
   [alert show];
}

Whatsapp is opened without any text for text sharing and image is also not getting shared. Please help me.

Mrunal
  • 13,982
  • 6
  • 52
  • 96
Praveen
  • 45
  • 1
  • 11

2 Answers2

2

Did you add "LSApplicationQueriesSchemes" to you plist?

  1. Go to your project/Supporting Files/YourProjectName-Info.plist in XCode project Explorer.
  2. Add a new key-value pair. Key : LSApplicationQueriesSchemes Type : Array Add a new string item "whatsapp"

Please try this and let me know if it is still not working.

rixian
  • 41
  • 2
0

note:- You can only share text or image, both sharing together in whatsApp is not working from whatsApp side

 /*
    //Share text
    NSString *textToShare = @"Enter your text to be shared";
    NSArray *objectsToShare = @[textToShare];
    
    UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:objectsToShare applicationActivities:nil];
    [self presentViewController:activityVC animated:YES completion:nil];
     */
    
    //Share Image
    UIImage * image = [UIImage imageNamed:@"images"];
    NSArray *objectsToShare = @[image];
    
    UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:objectsToShare applicationActivities:nil];
    [self presentViewController:activityVC animated:YES completion:nil];
Community
  • 1
  • 1
Mehul
  • 3,033
  • 23
  • 37