Sendoa
Sendoa

Reputation: 4715

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!

Upvotes: 7

Views: 19191

Answers (3)

Dnyaneshwar Wakchaure
Dnyaneshwar Wakchaure

Reputation: 232

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
}

Upvotes: 2

Jay Bhalani
Jay Bhalani

Reputation: 4171

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];
    }

Upvotes: 10

Justin Youens
Justin Youens

Reputation: 116

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.)

Upvotes: 10

Related Questions