How to share content on WhatsApp from iOS

I would like to share one Url link and some WhatsApp text message from my app. How can I share content?

I got this text-only code

NSString * msg = @"Trueman India Magazine";
NSString * urlWhats = [NSString stringWithFormat:@"whatsapp://send?text=%@",msg];
NSURL * whatsappURL = [NSURL URLWithString:[urlWhats stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

if ([[UIApplication sharedApplication] canOpenURL: whatsappURL])
    {
        [[UIApplication sharedApplication] openURL: whatsappURL];
    }

      

but how can i share url link in whatsapp?

+3


source to share


3 answers


Include a simple link inside the text, for example:

NSString * msg = @"Trueman India Magazine http://www.truemanindiamagazine.com";

      



The link will be generated / displayed after sending it to someone

+1


source


I had a problem with this whatsapp api with url strings especially when they contained a query string with multiple fields eg. http://example.com/foo?bar=foo&foo=bar . When opening the app, I found that the message text would be blank.

The solution was to properly evade the string using CFString functions. See 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 problem, here's my complete solution:



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

      

  • Note the use of characters that must be escaped in the CFURLCreateStringByAddingPercentEscapes function.
+5


source


We can achieve this using simple jquery. here's a link to the article http://www.stepblogging.com/how-to-share-web-article-on-whatsapp-using-jquery/

and you can check the demo on your smartphone Demo link

0


source







All Articles