문제

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!"];
도움이 되었습니까?

해결책 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.

다른 팁

Matteo Pacini your answer is not correct,

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

string = [string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top