Share YouTube Link in WhatsApp - ios

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?

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

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.

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

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.

Related

Open apple maps on ios 8 and above not working

Im trying to create an application which will open apple maps located in iOS device with given source and destination address.
NSString* addr = [NSString stringWithFormat: #"http://maps.apple.com/?daddr=%#&saddr=%#",[_fromTextfield.text stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]],[_toTextfield.text stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]]];
addr=[addr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]];
NSURL* url = [NSURL URLWithString:addr];
if ([[UIApplication sharedApplication]canOpenURL:url])
{
[[UIApplication sharedApplication] openURL:url];
}
But canOpenURL is not working ! It always returns NO.
I have added
"LSApplicationQueriesSchemes
urlscheme
urlscheme2
urlscheme3
urlscheme4
"
in Info.plist file.
Try below coding.it works perfectly.
NSString* addr = [NSString stringWithFormat: #"http://maps.apple.com/?daddr=%#&saddr=%#",#"Lacock" ,#"Avebury"];
NSURL* url = [NSURL URLWithString:addr];
if ([[UIApplication sharedApplication]canOpenURL:url])
{
[[UIApplication sharedApplication] openURL:url];
}
If error is
"This app is not allowed to query for scheme whatsapp"
1) Check Info plist.
add LSApplicationQueriesSchemes Array
add whatsapp String.
If error is invalid url.
2) Check, whether the string you are passing is not having special characters. Specially whitespace.
string = [string stringByReplacingOccurrencesOfString:#" " withString:#"%20"];
then use this string for url.
Both of these should solve. Cheers!

How to open App in iOS using "appname://openApp?"?

I need to open another app with parameters from my iOS App.
I have specific URL scheme provided from the developers of the App that have to be opened:
secondApp://openApp?param1=XXX&param2=YYY
I try to search Google how to open App in this way but I did not found any example how to use this construct.
Can you provide me with link or a row of code how to open App in this way?
You can look into this documentation of Inter-App Communication provided by Apple. Also, you can look into this tutorial. Here, they are sending text to another app. Also, another detailed answer to your question can be found in here.
Following code from the tutorial will help you :
-(IBAction) openReceiverApp {
// Opens the Receiver app if installed, otherwise displays an error
UIApplication *ourApplication = [UIApplication sharedApplication];
NSString *ourPath = #"secondApp://openApp?param1=XXX";
NSURL *ourURL = [NSURL URLWithString:ourPath];
if ([ourApplication canOpenURL:ourURL]) {
[ourApplication openURL:ourURL];
}
}
To open second app from your iOS app, just use:
NSURL *url = [NSURL URLWithString:#"secondApp://openApp?param1=XXX&param2=YYY"]; //fill your params
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
}

Make a phone call from UIWebView without prompt

SCENARIO
I have an app that is a UIWebView, I make some url overriding for requirements.
PROBLEM
To make a call opening url with tel: works weird in iOS7 and iOS8, it makes the phone call direct in the background, but it also ask for the confirmation, so user experience is horrible:
[[UIApplication sharedApplication] openURL:request.URL];
SOLUTION
To solve this issue, I used telprompt. It works nice in all iOS versions:
NSURL *url = [NSURL URLWithString:#"telprompt://637****"];
return [[UIApplication sharedApplication] openURL:url];
But shows this confirmation dialog:
QUESTION
Now, I have a new requirement, to make the phone call without confirmation or prompt. So... There is some way to make a phone call in iOS omitting the confirmation prompt?
I want something like
NSURL *url = [NSURL URLWithString:#"telnoprompt://637******"];
return [[UIApplication sharedApplication] openURL:url];
NSMutableCharacterSet *characterSet =[NSMutableCharacterSet characterSetWithCharactersInString:#" "];
NSArray *arrayOfComponents = [phone_number componentsSeparatedByCharactersInSet:characterSet];
phone_number = [arrayOfComponents componentsJoinedByString:#""];
NSString *phoneURLString = [NSString stringWithFormat:#"tel:%#", phone_number];
NSString *escapedUrlString = [phoneURLString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *phoneURL = [NSURL URLWithString:escapedUrlString];

How to share content on WhatsApp from iOS

I would like to share one Url link and some text message into WhatsApp from my application. How can i share content?
I got this code for only text
NSString * msg = #"Trueman India Magazine";
NSString * urlWhats = [NSString stringWithFormat:#"whatsapp://send?text=%#",msg];
NSURL * whatsappURL = [NSURL URLWithString:[urlWhats stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
if ([[UIApplication sharedApplication] canOpenURL: whatsappURL])
{
[[UIApplication sharedApplication] openURL: whatsappURL];
}
but how i share my url link in WhatsApp?
I had a problem with this whatsapp api with url strings, especially when they contained a query string with several fields, e.g. http://example.com/foo?bar=foo&foo=bar.
When opening the app I found the message text would be empty.
The solution was to properly percent escape the string using the CFString functions.
See the apple documentation here:
https://developer.apple.com/library/mac/documentation/CoreFoundation/Reference/CFURLRef/index.html#//apple_ref/c/func/CFURLCreateStringByAddingPercentEscapes
But for anyone else with this issue here is my solution in full:
CFStringRef originalURLString = (__bridge CFStringRef)[NSString stringWithFormat:#"%#", #"http://example.com/foo?bar=foo&foo=bar"];
CFStringRef preprocessedURLString = CFURLCreateStringByReplacingPercentEscapesUsingEncoding(kCFAllocatorDefault, originalURLString, CFSTR(""), kCFStringEncodingUTF8);
NSString *urlString = (__bridge NSString*)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, preprocessedURLString, NULL, CFSTR("!*'();:#&=+$,/?%#[]"), kCFStringEncodingUTF8);
NSString *whatsAppURLString = [NSString stringWithFormat:#"whatsapp://send?text=%#", urlString];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:whatsAppURLString]];
Note the use of the characters to be escaped in the CFURLCreateStringByAddingPercentEscapes function.
Include the plain link inside the text, e.g.:
NSString * msg = #"Trueman India Magazine http://www.truemanindiamagazine.com";
The link will be generated/tappable after sending it to someone
We can achieve this by using simple jquery. here is the article link http://www.stepblogging.com/how-to-share-web-article-on-whatsapp-using-jquery/
and you can check demo on your smart phone Demo Link

Open Facebook url in Safari instead of native app

is there any way to open Facebook url (ex. http://www.facebook.com/facebook) in Safari instead of native app? I'm tried to do this:
NSURL *url = [NSURL URLWithString:#"http://www.facebook.com/facebook"];
[[UIApplication sharedApplication] openURL:url];
but iOS automatically launch native client (of course if it's installed) if you try to open url with in facebook domain. thanks.
Ok, I think I found answer, you must replace "www.facebook.com" with "facebook.com" in url.
Something like this:
NSString *facebookUrlString = #"http://www.facebook.com/facebook";
if ([[facebookUrlString pathComponents] count] > 0) {
if ([[facebookUrlString pathComponents][1] isEqualToString:#"www.facebook.com"]) {
NSMutableArray *pathComponents = [[facebookUrlString pathComponents] mutableCopy];
[pathComponents replaceObjectAtIndex:1 withObject:#"facebook.com"];
facebookUrlString = [NSString pathWithComponents:pathComponents];
}
}
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:facebookUrlString]];
Copy link to the messenger app and send to yourself. Open the link on messenger app and click on share and will apear open with facebook app

Resources