无法在ios8中共享whatsapp上的url链接

时间:2014-12-23 08:58:54

标签: xcode youtube ios8 whatsapp

我想在whatsapp上分享一个网址,但它不起作用。如果我分享任何文本它工作正常。 这是我的代码。

    NSString *msg = @"https://www.youtube.com/user/kidstvabcd";
    NSString *urlWhats = [NSString stringWithFormat:@"whatsapp://send?text=%@",msg];
    NSURL *whatsappURL = [NSURL URLWithString:[urlWhats    stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    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 :(得分:4)

 NSString *theTempMessage = @"https://www.youtube.com/user/kidstvabcd";

    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"];

    NSString* theFinalMessage = [theTempMessage stringByReplacingOccurrencesOfString:@"&" withString:@"%26"];

    NSString * stringToSend=theFinalMessage;

    NSURL *whatsappURL = [NSURL URLWithString:stringToSend];

    if ([[UIApplication sharedApplication] canOpenURL: whatsappURL])

    {
        [[UIApplication sharedApplication] openURL: whatsappURL];
    }
    else
    {

       //any alert message
    }

答案 1 :(得分:0)

如果你这样做会更好:

 msg = [msg stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

这将在需要的地方添加转义百分比。