无法在whatsapp中与字符串共享url

时间:2015-10-02 12:47:59

标签: ios text ios8 share whatsapp

我试图通过WhatsApp分享文字+网址,但没有在WhatsApp文本字段中填充任何内容。

我尝试过以下代码:

NSString *urlString = [NSString stringWithFormat:@"http://www.mouthshut.com/contest?ref=%@&inv_via=whatsapp&act=reg", UserName];

//NSURL *url = [NSURL URLWithString:urlString];
//urlString = [urlString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

NSString * urlWhats = [NSString stringWithFormat:@"whatsapp://send?text=Writing a review is better than whatsapp. Win cash prizes daily on MouthShut.com \n Join Now %@", urlString];

NSURL * whatsappURL = [NSURL URLWithString:[urlWhats stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

if ([[UIApplication sharedApplication] canOpenURL: whatsappURL]) {
  [[UIApplication sharedApplication] openURL: whatsappURL];
} else {
  UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Alert!" message:@"Your device has no WhatsApp installed." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
  [alert show];
}

1 个答案:

答案 0 :(得分:0)

 NSString *urlString = [NSString stringWithFormat:@"http://www.mouthshut.com/contest?ref=%@&inv_via=whatsapp&act=reg", UserName];    
        urlString = [urlString stringByReplacingOccurrencesOfString:@":" withString:@"%3A"];
        urlString = [urlString stringByReplacingOccurrencesOfString:@"/" withString:@"%2F"];
        urlString = [urlString stringByReplacingOccurrencesOfString:@"?" withString:@"%3F"];
        urlString = [urlString stringByReplacingOccurrencesOfString:@"," withString:@"%2C"];
        urlString = [urlString stringByReplacingOccurrencesOfString:@"=" withString:@"%3D"];
        urlString = [urlString stringByReplacingOccurrencesOfString:@"&" withString:@"%26"];

    //NSURL *url = [NSURL URLWithString:urlString];
    //urlString = [urlString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    NSString * urlWhats = [NSString stringWithFormat:@"whatsapp://send?text=Writing a review is better than whatsapp. Win cash prizes daily on MouthShut.com \n Join Now %@", urlString];

    NSURL * whatsappURL = [NSURL URLWithString:[urlWhats stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

    if ([[UIApplication sharedApplication] canOpenURL: whatsappURL]) {
      [[UIApplication sharedApplication] openURL: whatsappURL];
    } else {
      UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Alert!" message:@"Your device has no WhatsApp installed." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
      [alert show];
    }