2
votes

I want to send a text message ("e.g. Hello World") to a particular whatsapp user from my app.

Base on this whatsapp FAQ link, I can just use the custom URL scheme to launch whatsapp to a particular contact number with a predefine text:

Question:

1) But where to get the contact ID, abid?:

2) How do I know if the contact number has whatsapp in the first place [for iOS we can't access it because of sandbox limitation right?] ?

3) How to insert the contact ID into the URL:

NSURL *whatsappURL = [NSURL URLWithString:@"whatsapp://send?text=Hello%2C%20World!"];

if ([[UIApplication sharedApplication] canOpenURL: whatsappURL]) {
    [[UIApplication sharedApplication] openURL: whatsappURL];
}
1

1 Answers

-2
votes

You can UIactivityViewController instead, so apps that support your text will appear:

NSString * text = @"Hello World!!";
NSArray * itemsArray = @[text];
NSArray * applicationActivities = nil;

UIActivityViewController * activityViewController = [[UIActivityViewController alloc] initWithActivityItems:itemsArray applicationActivities:applicationActivities];

activityViewController.excludedActivityTypes = @[
                                                 UIActivityTypePostToWeibo,
                                                 UIActivityTypeAssignToContact,
                                                 UIActivityTypeAirDrop,
                                                 UIActivityTypeAddToReadingList,
                                                 // Whatever you want prevent to appear...
                                                 ];

activityViewController.popoverPresentationController.sourceView = self.view;

[self presentViewController:activityViewController animated:YES completion:nil];

You are also able to add images, etc.