YosiFZ
YosiFZ

Reputation: 7900

Share YouTube Link in WhatsApp

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?

Upvotes: 7

Views: 8339

Answers (4)

Vishnu Kumar. S
Vishnu Kumar. S

Reputation: 1837

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.

Upvotes: 1

Alkalouti
Alkalouti

Reputation: 141

str = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL,
                                                              (CFStringRef)str,
                                                              NULL,
                                                              CFSTR("!*'();:@&=+$,/?%#[]"),
                                                              kCFStringEncodingUTF8));

Upvotes: 1

Fahim Parkar
Fahim Parkar

Reputation: 31637

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.

Upvotes: 1

YosiFZ
YosiFZ

Reputation: 7900

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);

Upvotes: 3

Related Questions