Question

I am using following code to share text on WhatsApp from my iOS app.

        NSString *textToSend = [NSString stringWithFormat:@"whatsapp://send?text=%@", self.theTextView.text];
        NSURL *whatsappURL = [NSURL URLWithString:textToSend];

        if ([[UIApplication sharedApplication] canOpenURL: whatsappURL]) {
            [[UIApplication sharedApplication] openURL: whatsappURL];
        }else{
            [[[UIAlertView alloc] initWithTitle:nil message:@"Whatsapp not isntalled on this device! Please install first." delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil, nil]show];
        }

This is not working as expected.

If I do like explained here it works fine.

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

La solution 2

I bet that self.theTextView.text is not getting URL-encoded.

How to solve that:

string = [string stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

Assuming that you're using UTF8.

Autres conseils

Matteo Pacini your answer is not correct,

you have to ADD PercentEscapes (stringByAddingPercentEscapesUsingEncoding:) NOT REPLACE PercentEscapes (stringByReplacingPercentEscapesUsingEncoding:)

string = [string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top