如何在iOS上分享WhatsApp上的内容

我想从我的应用程序分享一个Url链接和一些文本消息到WhatsApp。 我怎样才能分享内容?

我只有文本的代码

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

但是我怎样在WhatsApp中分享我的url链接呢?

在文本中包含简单链接,例如:

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

该链接发送给某人后将生成/可点击

我有一个问题与这个whatsapp API urlstring,尤其是当他们包含一个查询string与几个字段,例如http://example.com/foo?bar=foo&foo=bar 。 当打开应用程序,我发现消息文本将是空的。

解决方法是正确使用CFString函数对string进行转义。 请参阅苹果文档: https : //developer.apple.com/library/mac/documentation/CoreFoundation/Reference/CFURLRef/index.html#//apple_ref/c/func/CFURLCreateStringByAddingPercentEscapes

但对于这个问题的任何人这里是我的解决scheme:

 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]]; 
  • 请注意在CFURLCreateStringByAddingPercentEscapes函数中使用要转义的字符。

我们可以通过使用简单的jQuery来实现这一点。 这里是文章链接http://www.stepblogging.com/how-to-share-web-article-on-whatsapp-using-jquery/

您可以在您的智能手机Demo Link上查看演示