Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending an URL alongside text using WhatsApp URL scheme

I'm trying to send some text accompanied by an URL using WhatsApp's custom URL scheme. There's apparently only one valid parameter for this purpose: text:

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

The problem comes when I want to append my own URL to that text. I opted to encode it using this:

NSString *encodedURLString = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(
                                                                                  NULL,
                                                                                  (CFStringRef)urlAbsoluteString,
                                                                                  NULL,
                                                                                  (CFStringRef)@"!*'();:@&=+$,/?%#[]",
                                                                                  kCFStringEncodingUTF8 ));

The URL is sent to WhatsApp alongside the text but it doesn't get decoded on the WhatsApp's side:

WhatsApp not decoding the URL

Any ideas? Thank you!

like image 769
Sendoa Avatar asked Jul 31 '13 09:07

Sendoa


3 Answers

You're approaching it correctly, but it appears that the URL is being double-encoded. Make sure both the message and URL is only encoded once.

Using your same encoding method, you can do something like so:

NSString *urlAbsoluteString = @"Hello World! http://yayvisitmysiteplease.com?funky=parameter&stuff";
NSString *encodedURLString = ...

That should give you the URL to execute:

whatsapp://send?text=Hello%20World%21%20http%3A%2F%2Fyayvisitmysiteplease.com%3Ffunky%3Dparameter%26stuff

That makes its way into WhatsApp just like you'd expect. (I verified to make double sure.)

like image 168
Justin Youens Avatar answered Nov 14 '22 11:11

Justin Youens


This is complete code to send text and URL both in WhatsApp

    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];
    }
like image 10
Jay Bhalani Avatar answered Nov 14 '22 13:11

Jay Bhalani


It will work for Share Link on Whats app

NSString * url = [NSString stringWithFormat:@"http://video...bla..bla.."];
url =    (NSString*)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL,(CFStringRef) url, NULL,CFSTR("!*'();:@&=+$,/?%#[]"),kCFStringEncodingUTF8));

NSString * urlWhats = [NSString stringWithFormat:@"whatsapp://send?text=%@",url];
NSURL * whatsappURL = [NSURL URLWithString:urlWhats];
if ([[UIApplication sharedApplication] canOpenURL: whatsappURL]) {
 [[UIApplication sharedApplication] openURL: whatsappURL];
 } else {
 // can not share with whats app
}
like image 2
Dnyaneshwar Wakchaure Avatar answered Nov 14 '22 12:11

Dnyaneshwar Wakchaure