I would like to share one Url link and some text message into WhatsApp from my application. How can i share content?
I got this code for only text
NS
I had a problem with this whatsapp api with url strings, especially when they contained a query string with several fields, e.g. http://example.com/foo?bar=foo&foo=bar. When opening the app I found the message text would be empty.
The solution was to properly percent escape the string using the CFString functions. See the apple documentation here: https://developer.apple.com/library/mac/documentation/CoreFoundation/Reference/CFURLRef/index.html#//apple_ref/c/func/CFURLCreateStringByAddingPercentEscapes
But for anyone else with this issue here is my solution in full:
CFStringRef originalURLString = (__bridge CFStringRef)[NSString stringWithFormat:@"%@", @"http://example.com/foo?bar=foo&foo=bar"];
CFStringRef preprocessedURLString = CFURLCreateStringByReplacingPercentEscapesUsingEncoding(kCFAllocatorDefault, originalURLString, CFSTR(""), kCFStringEncodingUTF8);
NSString *urlString = (__bridge NSString*)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, preprocessedURLString, NULL, CFSTR("!*'();:@&=+$,/?%#[]"), kCFStringEncodingUTF8);
NSString *whatsAppURLString = [NSString stringWithFormat:@"whatsapp://send?text=%@", urlString];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:whatsAppURLString]];