使用WhatsApp URLscheme发送URL旁边的URL

我正在尝试使用WhatsApp的自定义URLscheme发送一些伴随URL的文本。 显然这只是一个有效的参数: text

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

当我想将自己的URL附加到该文本时,问题就出现了。 我select使用这个编码它:

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

该URL被发送到WhatsApp旁边的文本,但它不会被解码的WhatsApp的一面:

WhatsApp不解码的URL

有任何想法吗? 谢谢!

你正在接近它,但似乎是双重编码的URL。 确保邮件和url只编码一次。

使用相同的编码方法,你可以这样做:

 NSString *urlAbsoluteString = @"Hello World! http://yayvisitmysiteplease.com?funky=parameter&stuff"; NSString *encodedURLString = ... 

这应该给你的URL执行:

 whatsapp://send?text=Hello%20World%21%20http%3A%2F%2Fyayvisitmysiteplease.com%3Ffunky%3Dparameter%26stuff 

这就像你所期望的那样进入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]; } 

它将工作分享链接什么应用程序

 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 }