ios - whatsapp 共享不工作(相同的代码在另一个项目中工作)

标签 ios objective-c share whatsapp

下面是我用来在 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

最佳答案

这是您的解决方案。问题在于 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];
}

希望对您有所帮助!

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

关于ios - whatsapp 共享不工作(相同的代码在另一个项目中工作),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37373858/

相关文章:

ios - FooterView 仅在特定的展开部分?

iOS TableViewController在PopOver触摸tableview后消失

java - Android 共享和打开 XML 文件

ios - UICollectionView - 连接到自定义单元格

iOS:未调用 CLLocationManager 的 didEnterRegion

objective-c - 为什么 AVAssetExportSession 会产生一个空文件?

ios - 将 UIImageview 添加到 subview 后无法单击 UIAlertView 中的按钮

iphone - 当调用 NSSet 的 setByAddingX 方法时,哪个集合的对象获胜?

linux - 有什么办法可以防止不同的用户删除同一共享文件夹中其他用户的文件(但他们可以创建自己的新文件)?

iphone - "Gallery"app在IOS中分享 View