简体   繁体   English

使用WhatsApp URL方案在文本旁边发送URL

[英]Sending an URL alongside text using WhatsApp URL scheme

I'm trying to send some text accompanied by an URL using WhatsApp's custom URL scheme. 我正在尝试使用WhatsApp的自定义URL方案发送一些带有URL的文本。 There's apparently only one valid parameter for this purpose: text : 显然只有一个有效参数用于此目的: text

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

The problem comes when I want to append my own URL to that text. 当我想将自己的URL附加到该文本时,就会出现问题。 I opted to encode it using this: 我选择使用它编码它:

NSString *encodedURLString = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(
                                                                                  NULL,
                                                                                  (CFStringRef)urlAbsoluteString,
                                                                                  NULL,
                                                                                  (CFStringRef)@"!*'();:@&=+$,/?%#[]",
                                                                                  kCFStringEncodingUTF8 ));

The URL is sent to WhatsApp alongside the text but it doesn't get decoded on the WhatsApp's side: 该URL与文本一起发送到WhatsApp,但不会在WhatsApp方面解码:

WhatsApp没有解码URL

Any ideas? 有任何想法吗? Thank you! 谢谢!

You're approaching it correctly, but it appears that the URL is being double-encoded. 您正确地接近它,但似乎URL正在进行双重编码。 Make sure both the message and URL is only encoded once. 确保消息和URL仅编码一次。

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: 这应该给你执行的URL:

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. 这就像你期望的那样进入WhatsApp。 (I verified to make double sure.) (我证实了双重确定。)

This is complete code to send text and URL both in WhatsApp 这是在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];
    }

It will work for Share Link on Whats app 它适用于Whats应用程序上的Share Link

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
}

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

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