7

I am trying to share a YouTube link in Whats App with :

NSURL *whatsappURL = [NSURL URLWithString:@"whatsapp://send?text=http://www.youtube.com/watch?v=lWA2pjMjpBs"];
if ([[UIApplication sharedApplication] canOpenURL: whatsappURL]) {
    [[UIApplication sharedApplication] openURL: whatsappURL];
}

But when Whats App opens the message box is empty. Any idea why this happens?

zmarties
  • 4,809
  • 22
  • 39
YosiFZ
  • 7,792
  • 21
  • 114
  • 221

4 Answers4

3

I found the answer if someone have the same problem:

You just need to encode the url:

NSString *str = [NSString stringWithFormat:youTubeLink,videoId];

str = (NSString *)CFURLCreateStringByAddingPercentEscapes(NULL,
                                                            (CFStringRef)str,
                                                            NULL,
                                                            CFSTR("!*'();:@&=+$,/?%#[]"),
                                                            kCFStringEncodingUTF8);
YosiFZ
  • 7,792
  • 21
  • 114
  • 221
  • but still whatsapp will ask the number where you want to send link. – Fahim Parkar Aug 22 '13 at 09:24
  • Yes i know,it's open whatsapp and the user can choose the number he want to send the link. the problem was that after he choose the msg box was empty.And after the decode the msg box include the youtube link – YosiFZ Aug 22 '13 at 09:26
1

For Encoding, use below.

NSString *str = [NSString stringWithFormat:@"http://www.youtube.com/watch?v=lWA2pjMjpBs" ];

str = [str stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

NSURL *whatsappURL = [NSURL URLWithString:[NSString stringWithFormat:@"whatsapp://send?text=%@", str]];
if ([[UIApplication sharedApplication] canOpenURL: whatsappURL]) {
    [[UIApplication sharedApplication] openURL: whatsappURL];
}

Its working... tested...

Note

You cannot send whatsapp on specific number. That is disadvantage we have.

For sending whatsapp message on specific number, it can be done below way.

NSURL *whatsappURL = [NSURL URLWithString:@"whatsapp://send?abid=1&text=Hello"];
                                                            ^^^^

Try with this code.

abid means Address Book ID. Now whatever is the number in your iPhone with id=1, it will choose this number.

The problem with abid is that abid for that number is NOT same in all iPhone. Means in your iPhone abid=1 is 12345 but in my iPhone abid=1 is 34567.

Also if that number is not saved in iPhone, you cannot send whatsapp link on that number directly from iOS App.

Fahim Parkar
  • 30,974
  • 45
  • 160
  • 276
1
str = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL,
                                                              (CFStringRef)str,
                                                              NULL,
                                                              CFSTR("!*'();:@&=+$,/?%#[]"),
                                                              kCFStringEncodingUTF8));
Alkalouti
  • 141
  • 1
  • 1
  • 8
1

I know this is an old post, but didn't find any accepted answer for the question, so I am posting my answer. Sometimes this may help someone.

In my application I tried to share the AppStore link of my app through WhatsApp. But WhatsApp opens the message box as empty. So I tried to send the link after encoding the link, thinking that WhatsApp is blocking the link as it contains special characters. But it also not worked for me.

At last I found a solution by shortening the link by using Bitly. You can create a short link for any links by using Bitly and can share these links to WhatsApp without having any issue.

Vishnu Kumar. S
  • 1,797
  • 1
  • 15
  • 35