繁体   English   中英

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

[英]How to share content on WhatsApp from iOS

我想从我的应用程序中分享一个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字符串的whatsapp api有一个问题,特别是当它们包含一个包含多个字段的查询字符串时,例如http://example.com/foo?bar=foo&foo=bar 打开应用程序时,我发现消息文本为空。

解决方案是使用CFString函数正确地百分比转义字符串。 请参阅此处的苹果文档: https//developer.apple.com/library/mac/documentation/CoreFoundation/Reference/CFURLRef/index.html#//apple_ref/c/func/CFURLCreateStringByAddingPercentEscapes

但对于有这个问题的其他人来说,这是我的完整解决方案:

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函数中使用要转义的字符。

在文本中包含普通链接,例如:

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

发送给某人后,该链接将被生成/发送

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

你可以在你的智能手机演示链接上查看演示

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM