【问题标题】:How to share content on WhatsApp from iOS如何从 iOS 分享 WhatsApp 上的内容
【发布时间】:2014-10-31 11:14:04
【问题描述】:

我想从我的应用程序中将一个 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 链接?

【问题讨论】:

  • 您是否尝试过将链接添加为像msg 这样的字符串?
  • 不,我不会尝试,因为在模拟器中没有安装什么应用程序。有没有可能在模拟器中安装什么应用程序?
  • 你为什么还要问你是否还没有尝试过呢?请在询问之前先尝试自己进行研究。不,你不能在模拟器上安装 whatsapp,你需要一个物理设备。

标签: ios whatsapp


【解决方案1】:

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

【讨论】:

  • 所有操作都可以正常工作,例如:+、-、*、/。但是当我使用 % 然后 WhatsAppURLString 返回 (null)
【解决方案2】:

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

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

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

【讨论】:

  • “检查一下”是要分享的文本示例
  • 好吧,看来您是在告诉他们检查 stackoverflow。请添加正确的代码,以向您展示任何人都可以转身说检查一下这是一个糟糕的答案的示例。
  • 好的,我改进了答案。感谢您的反馈!
  • 最好我已经删除了我的 -1
【解决方案3】:

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

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

【讨论】:

    猜你喜欢
    • 2017-04-16
    • 1970-01-01
    • 2017-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-16
    相关资源
    最近更新 更多