Sendoa Sendoa - 2 months ago 48
Objective-C Question

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!

Answer

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

Comments