Submit url along with text using WhatsApp url scheme

I am trying to send text followed by a url using the WhatsApp custom url scheme. There is only one valid parameter for this purpose: text :

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

The problem occurs when I want to add my own URL to this text. I decided to code it using this:

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

The URL is sent to WhatsApp along with the text, but it is not decoded on the WhatsApp side:

WhatsApp not decoding the URL

Any ideas? Thanks!

+7
ios objective-c cocoa whatsapp
source share
3 answers

You are approaching it correctly, but the URL seems to be twice encoded. Make sure that both the message and the URL are encoded only once.

Using the same encoding method, you can do something like this:

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

This should give you the url to execute:

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

It makes its way into WhatsApp just as you expected. (I checked to make double sure.)

+10
source share

This is the complete code for sending text and URLs as 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]; } 
+10
source share

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 } 
+2
source share

All Articles