Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Share link using whatsapp

I used these code for share app link in what's app but nothing is come in the textfield of whatsapp. If using simple text then its work. Can anyone suggest the final outcome.

NSString *theTempMessage = @"whatsapp://send?text=https://itunes.apple.com/in/app/myapp/id1054375332?ls=1&mt=8";
NSString *theFinalMessage;

theTempMessage = [theTempMessage stringByReplacingOccurrencesOfString:@":" withString:@"%3A"];
theTempMessage = [theTempMessage stringByReplacingOccurrencesOfString:@"/" withString:@"%2F"];
theTempMessage = [theTempMessage stringByReplacingOccurrencesOfString:@"?" withString:@"%3F"];
theTempMessage = [theTempMessage stringByReplacingOccurrencesOfString:@"," withString:@"%2C"];
theTempMessage = [theTempMessage stringByReplacingOccurrencesOfString:@"=" withString:@"%3D"];
theFinalMessage = [theTempMessage stringByReplacingOccurrencesOfString:@"&" withString:@"%26"];

NSString * stringToSend=theFinalMessage;
NSURL *whatsappURL = [NSURL URLWithString:stringToSend];

if ([[UIApplication sharedApplication] canOpenURL: whatsappURL])

{
    [[UIApplication sharedApplication] openURL: whatsappURL];
}
like image 498
CJ IOS Developer Avatar asked Dec 02 '15 07:12

CJ IOS Developer


People also ask

Can you share a link on WhatsApp?

Tap Invite via link. Choose to Send link via WhatsApp, Copy link, Share link through another app, or QR code. If sending through WhatsApp, search for or select contacts, then tap Send . To reset the link, tap Reset link > RESET LINK.

How do I send my link on WhatsApp?

Open the app and navigate to Settings > Business Tools > Short Links. Tap “short link” to view the link. Tap the link icon to copy the link and paste it into a message, social post, or add it to another website.

How can I add share button in WhatsApp?

This article describes how you can add WhatsApp share button in your website. Note: This will work only when website is open in mobile with WhatsApp installed. Step 1: Design a simple webpage with a hyperlink on it. Sharing will be done when user click on this link.


1 Answers

Add this to your Info.plist

<key>LSApplicationQueriesSchemes</key>
    <array>
        <string>whatsapp</string>
    </array>

Implement this code to the ViewController where you need to open WhatsApp for sharing. (like for example say a button action) Update for swift 3 version (Xcode 8.x) : Updated for deprecations:

var str = "This is the string which you want to share to WhatsApp"
str=str.addingPercentEncoding(withAllowedCharacters: (NSCharacterSet.urlQueryAllowed))!
let whatsappURL = URL(string: "whatsapp://send?text=\(str)")
if UIApplication.shared.canOpenURL(whatsappURL) {
   UIApplication.shared.open(whatsappURL!, options: [:], completionHandler: nil)
} else {
   showAlert(message: "Whatsapp is not installed on this device. Please install Whatsapp and try again.")
}

Here showAlert() is a custom function for showing an alert.

like image 197
Ankit Kumar Gupta Avatar answered Sep 22 '22 14:09

Ankit Kumar Gupta