使用WhatsApp URL方案发送我的应用URL方案

时间:2014-01-30 05:10:22

标签: ios objective-c url nsurl whatsapp

也许我在这里遗漏了一些简单的东西,但我不能为我的生活让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应用,但是在我选择了一个联系人之后,消息框没有预先填充,它仍然是空的。我做错了什么?

4 个答案:

答案 0 :(得分:3)

最后,我得到了将自己的应用程序itunes URL分享给whatsapp的解决方案。

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);

我测试得很好。 :) 祝你好运。

答案 1 :(得分: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];
    }

答案 2 :(得分:0)

你附加了字符串UTFEncoded吗?我的意思是这是你的解决方案。

答案 3 :(得分: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应该包含一些有效的东西。 请尝试一下,让我知道。

我希望这会有所帮助。