z22
z22

Reputation: 10083

iOS OpenURL queryString not sent

I want to share message from iOS app to whatsApp. For this I am using the following code:

NSString *strMsg = @"whatsapp://send?text=I%20found%20this.%20Check%20it%20out.Deal_1403803205.6628140%20http://MyiOSApp/register.php?deal_id=978&l_id=198&share=true&customer_id=34&domain=5";
    NSURL *whatsappURL = [NSURL URLWithString:strMsg];
    if ([[UIApplication sharedApplication] canOpenURL: whatsappURL]) {
        [[UIApplication sharedApplication] openURL: whatsappURL];
    } else {
        UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"" message:@"WhatsApp is not installed on your device" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
    }

The problem is that the message after the ? is not sent.

This works: @"whatsapp://send?text=I%20found%20this.%20Check%20it%20out.Deal_1403803205.6628140%20http://MyiOSApp/register.php

But this doesn't :

@"whatsapp://send?text=I%20found%20this.%20Check%20it%20out.Deal_1403803205.6628140%20http://MyiOSApp/register.php?deal_id=978&l_id=198&share=true&customer_id=34&domain=5";

Where am I getting wrong? How do I solve this?

Upvotes: 1

Views: 338

Answers (3)

zaph
zaph

Reputation: 112857

For iOS 7 and above:

NSString *escapedString = [unescaped stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]];

Upvotes: 0

rishi
rishi

Reputation: 11839

You have to encode it properly, use -

@"whatsapp://send?text=I%20found%20this.%20Check%20it%20out.Deal_1403803205.6628140%20http://MyiOSApp/register.php%3Fdeal_id=978&l_id=198&share=true&customer_id=34&domain=5";

"?" character is not encoded, you need to encode that as above.

Upvotes: 1

Lloyd Presly
Lloyd Presly

Reputation: 21

Yes as rishi said proper encoding very important.

To encode the message use the following code.

- (NSString *)getEncodedMessage:(NSString *)message
{
    CFStringRef encodedMessage = CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)message, NULL,
                                                                        (CFStringRef)@"!*'();:@&=+$,/?%#[]", kCFStringEncodingUTF8);
    return CFBridgingRelease(encodedMessage);
}

Hope this will help you.

Upvotes: 0

Related Questions