How to share content on WhatsApp from iOS - 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

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!

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

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?
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.

How to invoke E-mail application?

How to invokeE-mail application by code in ios system? I want to check email interface rather than sending mail interface.
Thanks.
You could make use of this:
- (void)launchMailAppOnDevice
{
NSMutableString *subject = [NSMutableString string];
[subject appendString:#"Your Subject"];
NSMutableString *mailbody = [NSMutableString string];
[mailbody appendString:#"Blah Blah Blah"];
[mailbody appendString:#"Blah Blah Blah "];
NSString *recipients = [NSString stringWithFormat:#"mailto:test#test.com?&subject=%#!",subject];
NSString *body = [NSString stringWithFormat:#"&body=%#!",mailbody];;
NSString *emailString = [NSString stringWithFormat:#"%#%#", recipients, body];
emailString = [emailString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];
}
If you want to check whether mail app is present int app and you want to open it.
if ([[UIApplication sharedApplication] canOpenURL:[ NSURL URLWithString:#"mailto:"]])
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"mailto:"]];
MFMailComposeViewController is an official API that you can use to compose messages from within your app.
I've heard you can use the "mailto:" URL scheme to open the Mail application and compose a message from there (assuming the Mail app is still installed on your iOS device, that is), but this is only supposed for composing a message from within the mail app, it doesn't switch the user to the inbox.
You can't open mail app. All you can do it open the compose e-mail interface using either MFMailComposeViewController or mailto: URL.

why use openURL open the google map can't find the address

I create a UITextView and set text=#"中国,浙江省杭州市滨江区",
set dataDetectorTypes=UIDataDetectorTypeAddress,
then,long pressed, choose open map, it can found the address in GoogleMap.
But, the same address, i used openUrl can't find the address.
NSString *urlText = [NSString stringWithFormat:#"http://maps.google.com/maps?q=%#", [address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlText]];
anybody who can tell me why? or iOS not use this url(http://maps.google.com/maps?q=%#)
Would you try with a different enconding? eg., NSUnicodeStringEncoding
NSString *urlText = [NSString stringWithFormat:#"http://maps.google.com/maps?q=%#", [address stringByAddingPercentEscapesUsingEncoding:NSUnicodeStringEncoding]];
And what is the result of
[address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
?
Google Maps http:// calls don't use % seperators but rather +'s.
NSString *fixedAddress = [fullAddress stringByReplacingOccurencesOfString:#" " withString:#"+"];
NSString *googleCall = #"http://maps.google.com/maps?q=";
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[googleCall stringByAppendingString:fixedAddress]]];
I, myself encountered this issue and fixed it with the preceeding code last night.

Resources