2

I am trying to send text message on whats app from my iOS application but its not success. I had added my code below, Please suggest me what I am doing wrong. My device has whats app installed, but still I am unable to send any text message.

- (IBAction)whatAppInvite:(id)sender
    {
        NSString * strTextPost = [@"" stringByAppendingFormat:@"Hey try this app. Its amazing. \n\n https://itunes.apple.com/us/app/google-search/*********2?mt=8"];


        strTextPost = [strTextPost stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
        strTextPost = [strTextPost stringByReplacingOccurrencesOfString:@":" withString:@"%3A"];
        strTextPost = [strTextPost stringByReplacingOccurrencesOfString:@"/" withString:@"%2F"];
        strTextPost = [strTextPost stringByReplacingOccurrencesOfString:@"?" withString:@"%3F"];
        strTextPost = [strTextPost stringByReplacingOccurrencesOfString:@"," withString:@"%2C"];
        strTextPost = [strTextPost stringByReplacingOccurrencesOfString:@"=" withString:@"%3D"];
        strTextPost = [strTextPost stringByReplacingOccurrencesOfString:@"&" withString:@"%26"];
        strTextPost = [strTextPost stringByReplacingOccurrencesOfString:@"." withString:@"%2E"];
        strTextPost = [strTextPost stringByReplacingOccurrencesOfString:@"'" withString:@"%27"];
        strTextPost = [strTextPost stringByReplacingOccurrencesOfString:@"-" withString:@"%2D"];

        NSString * urlWhats = [NSString stringWithFormat:@"whatsapp://send?text=%@",strTextPost];

        NSURL * whatsappURL = [NSURL URLWithString:urlWhats];
        if ([[UIApplication sharedApplication] canOpenURL: [NSURL URLWithString:@"whatsapp://app"]])
        {
            if ([[UIApplication sharedApplication] canOpenURL: whatsappURL])
            {
            [[UIApplication sharedApplication] openURL: whatsappURL];
            }
            else
            {
                // don't know what happens
                // calls this
            }
        }
        else
        {
            UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"WhatsApp not installed." message:@"Your device has no WhatsApp installed." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alert show];
        }

    }
Shamas S
  • 7,507
  • 10
  • 46
  • 58
Aarti Oza
  • 1,134
  • 2
  • 14
  • 31
  • check this answer : http://stackoverflow.com/questions/17966461/sending-an-url-alongside-text-using-whatsapp-url-scheme/27647174#27647174 – Jay Bhalani Dec 14 '15 at 06:41

3 Answers3

7

try this code :

NSString * msg = @"Application%20Name%20https://itunes.apple.com/YOUR-URL";

msg = [msg stringByReplacingOccurrencesOfString:@":" withString:@"%3A"];
msg = [msg stringByReplacingOccurrencesOfString:@"/" withString:@"%2F"];
msg = [msg stringByReplacingOccurrencesOfString:@"?" withString:@"%3F"];
msg = [msg stringByReplacingOccurrencesOfString:@"," withString:@"%2C"];
msg = [msg stringByReplacingOccurrencesOfString:@"=" withString:@"%3D"];
msg = [msg stringByReplacingOccurrencesOfString:@"&" withString:@"%26"];

NSString * urlWhats = [NSString stringWithFormat:@"whatsapp://send?text=%@",msg];
NSURL * whatsappURL = [NSURL URLWithString:urlWhats];
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];
}
Jay Bhalani
  • 4,142
  • 8
  • 37
  • 50
1

use UIActivityViewController to send the message

Birendra
  • 623
  • 1
  • 5
  • 17
0

check this out "Share link using whatsapp"

Dont do string replacement when you try to send message through whatsapp.

Community
  • 1
  • 1
satheesh
  • 2,770
  • 2
  • 14
  • 19