Fahim Parkar Fahim Parkar - 6 months ago 15
Objective-C Question

whatsapp share is not working (same code is working in another project)

Below is what I am using to share text on 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];
}


Below is the output I have of NSLog.

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

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


When I click WhatsApp icon, its opening WhatsApp but message is not showing in showing... It is just showing blank.

Any idea why this is working?

Note: Same code is working is another project of mine which is updated on 09 May 2016.




Edit 1



Even if I try below still its not working

This is test text

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

Answer

Here is your solution . The problem is with the URL encoding. Please find my tested solution below

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

Hope this Helps!.

let me know if you find any difficulty.