使用WhatsApp URL Scheme发送我的应用程序URL Scheme

3

也许我错过了一些简单的东西,但是我无论如何都无法使用URL方案在WhatsApp应用程序上显示我的URL。 我有:

        NSString *stringToSend = [[NSString stringWithFormat:@"whatsapp://send?text=myAppDomain://%@moreChars",specialString] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

        NSURL *whatsappURL = [NSURL URLWithString:stringToSend];

        [[UIApplication sharedApplication] openURL: whatsappURL];

它成功启动了 WhatsApp 应用,但是在我选择联系人后,消息框没有被预填,仍然为空。我做错了什么?

如果您发送其他文本,它是否有效?(即不是您的URL) - James Frost
是的,它可以。当我硬编码单词或使用stringWithFormat时,它都能正常工作。 - JustAnotherCoder
我也遇到了同样的问题。你找到解决方案了吗? - Michel
@Michel 不幸的是,我还没有。但自从三月以来我没有尝试过。 - JustAnotherCoder
4个回答

3
最终,我找到了一种方法来在WhatsApp上分享自己的应用程序iTunes链接。
NSString *string = [@"" stringByAppendingFormat:@"Share this on whatsapp \n\n https://itunes.apple.com/us/app/google-search/id284815942?mt=8"];

 string = [string stringByReplacingOccurrencesOfString:@"=" withString:@"%3D"];
    string = [string stringByReplacingOccurrencesOfString:@"&" withString:@"%26"];

  NSLog(@">>>Just pass this to Whatspp using their URL scheme %@",string);

我测试过这个功能,:) 祝你好运。


2
这是发送文本和链接到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];
    }

0

使用WhatsApp时,仅使用stringByAddingPercentEscapesUsingEncoding是不够的。尝试以下代码,这对我有效:

NSString *theTempMessage,*theFinalMessage;
.......
    /// theTempMessage should contain your stringToSend (in its current state);
    theTempMessage = [theTempMessage stringByReplacingOccurrencesOfString:@":" withString:@"%3A"];
    theTempMessage = [theTempMessage stringByReplacingOccurrencesOfString:@"/" withString:@"%2F"];
    theTempMessage = [theTempMessage stringByReplacingOccurrencesOfString:@"?" withString:@"%3F"];
    theTempMessage = [theTempMessage stringByReplacingOccurrencesOfString:@"," withString:@"%2C"];
    theTempMessage = [theTempMessage stringByReplacingOccurrencesOfString:@"=" withString:@"%3D"];
    theFinalMessage = [theTempMessage stringByReplacingOccurrencesOfString:@"&" withString:@"%26"];
    stringToSend=theFinalMessage;

此时,您的stringToSend应该包含一些可用的内容。请尝试并让我知道。

希望这可以帮助到您。


0

你的附加字符串是否采用UTF编码?我的意思是这是你的解决方案。


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