Skip to content

Instantly share code, notes, and snippets.

@sohelsd
Last active January 21, 2023 23:13
Show Gist options
  • Save sohelsd/d7a391d9716255109e72 to your computer and use it in GitHub Desktop.
Save sohelsd/d7a391d9716255109e72 to your computer and use it in GitHub Desktop.
WhatsApp Swift UIActivityViewController Custom UIActivity
How to add WhatsApp to UIActivityViewController?
Drop the Whatsapp.swift file in your project.
Initialize the controller as described in ViewController.swift
//HOW TO USE THE WHATSAPP SHARE ACTION
@IBAction func shareAction(sender: UIButton) {
let objectsToShare = [message]
let wsActivity = WhatsAppActivity()
let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: [wsActivity])
activityVC.excludedActivityTypes = [UIActivityTypeAirDrop, UIActivityTypeAddToReadingList,UIActivityTypeCopyToPasteboard,UIActivityTypeSaveToCameraRoll,UIActivityTypePrint]
self.presentViewController(activityVC, animated: true, completion: nil)
}
//
// Whatsapp.swift
// Send2Phone
//
// Created by Sohel Siddique on 3/27/15.
// Copyright (c) 2015 Zuzis. All rights reserved.
//
import UIKit
class WhatsAppActivity : UIActivity{
override init() {
self.text = ""
}
var text:String?
override func activityType()-> String {
return NSStringFromClass(self.classForCoder)
}
override func activityImage()-> UIImage
{
return UIImage(named: "whatsapp2")!;
}
override func activityTitle() -> String
{
return "WhatsApp";
}
override class func activityCategory() -> UIActivityCategory{
return UIActivityCategory.Share
}
func getURLFromMessage(message:String)-> NSURL
{
var url = "whatsapp://"
if (message != "")
{
url = "\(url)send?text=\(message)"
}
return NSURL(string: url)!
}
override func canPerformWithActivityItems(activityItems: [AnyObject]) -> Bool {
for activityItem in activityItems
{
if (activityItem.isKindOfClass(NSString))
{
self.text = activityItem as? String;
var whatsAppURL:NSURL = self.getURLFromMessage(self.text!)
return UIApplication.sharedApplication().canOpenURL(whatsAppURL)
}
}
return false;
}
override func prepareWithActivityItems(activityItems: [AnyObject]) {
for activityItem in activityItems{
if(activityItem.isKindOfClass(NSString)){
var whatsAppUrl:NSURL = self.getURLFromMessage(self.text!)
if(UIApplication.sharedApplication().canOpenURL(whatsAppUrl)){
UIApplication.sharedApplication().openURL(whatsAppUrl)
}
break;
}
}
}
}
@nikhilshekhawat
Copy link

nikhilshekhawat commented Apr 21, 2016

@artur that could be cuz of two reasons. Either the simulator does not contain whatsapp, or your code is not passing the expected message to the whatsapp share extension

Or that your canOpenUrl function is not working for which you would have to add this to you info.plist file:
"<''key''>LSApplicationQueriesSchemes<''/key>
<''array>
<''string>whatsapp<''/string>
<''/array>
<''/dict>"

@nicklee3333
Copy link

Hi, Sir,
I found few app now can share both image + text, do you have any idea how to implement. I can do only image (by UIdocumentInteractionController) or only text. Appreciate if you can teach me how to share photo + text to whatsapp.
Thanks in advanced.

@tzahola
Copy link

tzahola commented Mar 9, 2017

This is not how it's supposed to be done. You should only store the URL in prepareWithActivityItems and open it in performActivity.
See the docs on prepareWithActivityItems:

Subclasses should override this method and use it to store a reference to the data items in the activityItems parameter.

https://developer.apple.com/reference/uikit/uiactivity/1620668-preparewithactivityitems

@tejaskdubal
Copy link

its not working for swift 4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment