【问题标题】:whatsapp share is not working (same code is working in another project)whatsapp 共享不起作用(相同的代码在另一个项目中工作)
【发布时间】:2016-05-22 11:41:18
【问题描述】:

以下是我用来在 WhatsApp 上分享文字的内容

NSString *globalString;

NSString *myURl = [NSString stringWithFormat:@"http://www.mywebq8.com/mobile/newsdetails.aspx?id=%@", [global getstrProDetails]];

globalString =[NSString stringWithFormat: @"%@ للمزيد  \n\n%@",[global getstrPagetitle], myURl];

NSLog(@"globalString===%@", globalString);

NSString * msg = globalString;
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 *mAl = [[UIAlertView alloc] initWithTitle:@"" message:@"Your device doesn't have WhatsApp." delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
    [mAl show];
}

下面是我的 NSLog 输出。

حاول الانتحار فألقى بنفسه بين أسود.. وخرج حياً للمزيد  

http://www.mywebq8.com/mobile/newsdetails.aspx?id=22455

当我点击 WhatsApp 图标时,它会打开 WhatsApp,但显示中没有显示消息...它只是显示空白。

知道为什么会这样吗?

注意:我的另一个项目是相同的代码,它于 2016 年 5 月 9 日更新。


编辑 1

即使我在下面尝试仍然无法正常工作

This is test text

http://www.mywebq8.com/mobile/newsdetails.aspx?id=22455

【问题讨论】:

  • 您的代码是正确的,您的消息存在问题 NSString *myURl = [NSString stringWithFormat:@"mywebq8.com/mobile/newsdetails.aspx?id=22455"]; globalString =[NSString stringWithFormat: @"بين أسود.. وخرج حياً للمزيد للمزيد \n \n%@", myURl]; 如果您发送 NSString * msg=@"hello stackflow"; 它工作正常,请检查您的文本。

标签: ios objective-c share whatsapp


【解决方案1】:

这是您的解决方案。问题在于 URL 编码。 请在下面找到我测试过的解决方案

NSString *globalString;

NSString *myURl = [NSString stringWithFormat:@"http://www.mywebq8.com/mobile/newsdetails.aspx?id=%@", [global getstrProDetails]];

myURl = [myURl stringByReplacingOccurrencesOfString:@":" withString:@"%3A"];
myURl = [myURl stringByReplacingOccurrencesOfString:@"/" withString:@"%2F"];
myURl = [myURl stringByReplacingOccurrencesOfString:@"?" withString:@"%3F"];
myURl = [myURl stringByReplacingOccurrencesOfString:@"," withString:@"%2C"];
myURl = [myURl stringByReplacingOccurrencesOfString:@"=" withString:@"%3D"];
myURl = [myURl stringByReplacingOccurrencesOfString:@"&" withString:@"%26"];

globalString =[NSString stringWithFormat: @"%@ للمزيد  \n\n",[global getstrPagetitle]];

NSLog(@"globalString===%@", globalString);

NSString * urlWhats = [NSString stringWithFormat:@"whatsapp://send?text=%@", globalString ];
NSString *msg = [NSString stringWithFormat:@"%@%@",[urlWhats stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],myURl];

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

if ([[UIApplication sharedApplication] canOpenURL: whatsappURL]) {
    [[UIApplication sharedApplication] openURL: [NSURL URLWithString:msg]];
} else {
    UIAlertView *mAl = [[UIAlertView alloc] initWithTitle:@"" message:@"Your device doesn't have WhatsApp." delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
    [mAl show];
}

希望对您有所帮助!

如果您发现任何困难,请告诉我。

【讨论】:

  • 可能是该代码中的网址有所不同。
  • 这是说Your device doesn't have WhatsApp.
  • NSURL * whatsappURL = [NSURL URLWithString:urlWhats]; 应该是NSURL * whatsappURL = [NSURL URLWithString:msg];??
  • 是的,实际上我们分别用不同的方式对文本和url分别编码,然后将它们结合起来。你有什么困难吗?
  • 但是whatsappURL 里面没有url...那它怎么去watsapp 呢?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-09
  • 1970-01-01
  • 2017-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多