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 不解码 URL

有任何想法吗?谢谢!

4

3 回答 3

10

You're approaching it correctly, but it appears that the URL is being double-encoded. Make sure both the message and URL is only encoded once.

Using your same encoding method, you can do something like so:

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

That should give you the URL to execute:

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

That makes its way into WhatsApp just like you'd expect. (I verified to make double sure.)

于 2013-07-31T22:03:50.823 回答
10

这是在 WhatsApp 中发送文本和 URL 的完整代码

    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];
    }
于 2014-12-25T12:27:21.487 回答
2

它适用于 Whats 应用程序上的共享链接

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
}
于 2016-09-12T08:00:32.733 回答