Last active
January 21, 2023 23:13
-
-
Save sohelsd/d7a391d9716255109e72 to your computer and use it in GitHub Desktop.
WhatsApp Swift UIActivityViewController Custom UIActivity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
How to add WhatsApp to UIActivityViewController? | |
Drop the Whatsapp.swift file in your project. | |
Initialize the controller as described in ViewController.swift |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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; | |
} | |
} | |
} | |
} |
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
its not working for swift 4
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.