使用WhatsApp URL方案将URL与文本一起发送

7

我正在尝试使用WhatsApp的自定义URL方案发送一些文本和URL。显然,这个目的只有一个有效的参数:text

NSURL *whatsappURL = [NSURL URLWithString:@"whatsapp://send?text=Hello%2C%20World!"];

问题出现在我想将自己的URL附加到该文本时。我选择使用以下编码进行编码:
NSString *encodedURLString = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(
                                                                                  NULL,
                                                                                  (CFStringRef)urlAbsoluteString,
                                                                                  NULL,
                                                                                  (CFStringRef)@"!*'();:@&=+$,/?%#[]",
                                                                                  kCFStringEncodingUTF8 ));

URL会随着文本一起发送到WhatsApp,但WhatsApp不会对其进行解码:

WhatsApp not decoding the URL

有什么想法吗?谢谢!
3个回答

10

以下是完整的代码,可用于同时发送文本和URL到WhatsApp

    NSString * msg = @"Application%20Name%20https://itunes.apple.com/YOUR-URL";

    msg = [msg stringByReplacingOccurrencesOfString:@":" withString:@"%3A"];
    msg = [msg stringByReplacingOccurrencesOfString:@"/" withString:@"%2F"];
    msg = [msg stringByReplacingOccurrencesOfString:@"?" withString:@"%3F"];
    msg = [msg stringByReplacingOccurrencesOfString:@"," withString:@"%2C"];
    msg = [msg stringByReplacingOccurrencesOfString:@"=" withString:@"%3D"];
    msg = [msg stringByReplacingOccurrencesOfString:@"&" withString:@"%26"];

    NSString * urlWhats = [NSString stringWithFormat:@"whatsapp://send?text=%@",msg];
    NSURL * whatsappURL = [NSURL URLWithString:urlWhats];
    if ([[UIApplication sharedApplication] canOpenURL: whatsappURL])
    {
        [[UIApplication sharedApplication] openURL: whatsappURL];
    }
    else
    {
        UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"WhatsApp not installed." message:@"Your device has no WhatsApp installed." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
    }

1
这对我有用,而被接受的答案却不知为何没用。 - Murat Ögat

10

你的方法是正确的,但是URL似乎被双重编码了。请确保消息和URL只进行一次编码。

按照你使用的编码方法,可以这样做:

NSString *urlAbsoluteString = @"Hello World! http://yayvisitmysiteplease.com?funky=parameter&stuff";
NSString *encodedURLString = ...

那应该会给你执行的URL:

whatsapp://send?text=Hello%20World%21%20http%3A%2F%2Fyayvisitmysiteplease.com%3Ffunky%3Dparameter%26stuff

这就像你预期的那样进入WhatsApp。(我进行了双重确认。)


天啊!就是这个问题!在编码调用之后,我还有一个“隐藏”的stringByAddingPercentEscapesUsingEncoding:调用……天啊……谢谢你!!! - Sendoa

2

这将适用于Whats app上的共享链接

NSString * url = [NSString stringWithFormat:@"http://video...bla..bla.."];
url =    (NSString*)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL,(CFStringRef) url, NULL,CFSTR("!*'();:@&=+$,/?%#[]"),kCFStringEncodingUTF8));

NSString * urlWhats = [NSString stringWithFormat:@"whatsapp://send?text=%@",url];
NSURL * whatsappURL = [NSURL URLWithString:urlWhats];
if ([[UIApplication sharedApplication] canOpenURL: whatsappURL]) {
 [[UIApplication sharedApplication] openURL: whatsappURL];
 } else {
 // can not share with whats app
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接