Share PDF through WhatsApp - ios

I want to share a PDF directly to WhatsApp.
Below is the link I found to send text and image in WhatsApp in an iOS app, but unable to find that how to send PDF directly to WhatsApp.
Share image/text through WhatsApp in an iOS app

You can use Share Extension (UIActivityViewController) to share your pdf fileURL. Note that the user will have to select the WhatsApp application to share the file. Note is is required to edit your info.plist and add whatsapp to your LSApplicationQueriesSchemes array if you would like to check first if WhatsApp is installed:
func sharePdfWhatsApp(url: URL) {
let whatsappURL = URL(string:"whatsapp://app")!
// this will make sure WhatsApp it is installed
if UIApplication.shared.canOpenURL(whatsappURL) {
let controller = UIActivityViewController(activityItems: [url], applicationActivities: nil)
present(controller, animated: true) {
print("done")
}
}
}

First you have to retrieve your PDF file as a Data format.
var pdfDATA:Data!
Once you get data to above variable you can run below code to send PDF via WhatsApp.
self.pdfDATA = try? Data.init(contentsOf: yourFilepath)
let activitycontroller = UIActivityViewController(activityItems: [self.pdfDATA], applicationActivities: nil)
if activitycontroller.responds(to: #selector(getter: activitycontroller.completionWithItemsHandler))
{
activitycontroller.completionWithItemsHandler = {(type, isCompleted, items, error) in
if isCompleted
{
print("completed")
}
}
}
activitycontroller.excludedActivityTypes = [UIActivityType.airDrop]
activitycontroller.popoverPresentationController?.sourceView = buttonItemSize
self.present(activitycontroller, animated: true, completion: nil)

Related

Add custom message with the invitation link to download the app - Swift

I would like to know how you can add a custom message through the applications like Messages, Mail, Whatsapp, Snapchat... when a user shares the link to download the app (to invite a friend). My current code is :
#objc private func inviteButtonTapped() {
guard let url = URL(string: "https://www.google.com") else {
return
}
let shareSheetVC = UIActivityViewController(activityItems: [url], applicationActivities: nil)
present(shareSheetVC, animated: true)
}
I'm offering to share the link but when they select "Messages" for example, there's no custom message. Any idea ?

How do I share an image + text via WhatsApp?

I am trying to share an image + text via WhatsApp.
Here is my code:
let activityViewController = UIActivityViewController(activityItems: [imageNmae, "Share text"], applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = self
activityViewController.excludedActivityTypes = [ UIActivity.ActivityType.airDrop]
activityViewController.completionWithItemsHandler = {(activityType: UIActivity.ActivityType?, completed: Bool, returnedItems:[Any]?, error: Error?) in
self.shareMainView.isHidden = true
}
if let viewController = UIApplication.topMostViewController {
viewController.present(activityViewController, animated: true) {
// self.shareMainView.isHidden = true
}
}
How can I achieve this?
I can see some other apps sharing a URL and it's showing both an image and text. Even Android is able to share. But if execute the above code, I can only share text and not an image.
Share URL of the image along with your text.
WhatsApp will create preview from the link, and make it appear like image + text.
There is no real way of sending image + text on WhatsApp (in Nov 2021)

Swift: how to add App sharing feature with Extension Sheet

I want to add the my app sharing feature in my App. Like The Action Sheet Toggle up with the all the social Media and other app in which i can share my app Url. I try to find the tutorial for that but i can't find the proper tutorial for that. this is what I have Done.
func shareApp (){
let textToShare = "Swift is awesome! Check out this website about it!"
if let myWebsite = NSURL(string: "http://www.google.com/") {
let objectsToShare = [textToShare, myWebsite] as [Any]
let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
activityVC.excludedActivityTypes = [UIActivityType.airDrop, UIActivityType.addToReadingList]
self.present(activityVC, animated: true, completion: nil)
}
This What i Get.
So just want to show there Some Social Media options There.
All you have done is right, except you have not added the App url of your appstore link. Add that link like https://itunes.apple.com/in/app/more-customers-app/id1280868223?mt=8 and all your media will be appear in list if those application is installed in iphone.
You have got UIActivityViewController presented now, Just add in text like,
func ShareApp()
{
let items:[Any] = ["Marketplace of Building Material for Architects, Interior Designers, Contractors and Home Owners. Find Material for your next project. Download https://itunes.apple.com/in/app/more-customers-app/id1280868223?mt=8"]
let ac = UIActivityViewController(activityItems: items, applicationActivities: nil)
self.present(ac, animated: true)
}

How can i share a url like Safari do in my own app

I want to share a url like what Safari do in my own iOS app, How can i do this?
Use socail framework can share in some specified social plartform, but in my case ,I just want to share url like Safari.
Do you think on UIActivityViewController?
#IBAction func shareTextButton(_ sender: UIButton) {
// image to share
let link = "https://www.google.rs/"
// set up activity view controller
let activityViewController = UIActivityViewController(activityItems: [link], applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = self.view // so that iPads won't crash
// exclude some activity types from the list (optional)
activityViewController.excludedActivityTypes = [ UIActivityType.airDrop, UIActivityType.postToFacebook ]
// present the view controller
self.present(activityViewController, animated: true, completion: nil)
}

Share app link to by ActivityViewController iOS swift?

My app is not Live yet. I got the app ID from the App Store Connect. I want to share the app link on social media apps. I used the UIActivityViewController:
let string1 = "itms-apps://itunes.apple.com/app/idXXXXXXX"
let url = NSURL(string: string1)
let shareItems = [UIApplication.sharedApplication().openURL(url!)]
let activityViewController = UIActivityViewController(activityItems: shareItems, applicationActivities: nil)
self.presentViewController(activityViewController, animated: true, completion: nil)
Problem: It is not showing some social media apps like WhatsApp.
Solution for Swift 4 or better:
This solution also works on iPad (the solution above crashes on iPad):
if let urlStr = NSURL(string: "https://apps.apple.com/us/app/idxxxxxxxx?ls=1&mt=8") {
let objectsToShare = [urlStr]
let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
if UIDevice.current.userInterfaceIdiom == .pad {
if let popup = activityVC.popoverPresentationController {
popup.sourceView = self.view
popup.sourceRect = CGRect(x: self.view.frame.size.width / 2, y: self.view.frame.size.height / 4, width: 0, height: 0)
}
}
self.present(activityVC, animated: true, completion: nil)
}
This is used to open the site, not to share the app:
[UIApplication.sharedApplication().openURL(url!)]
Do this instead:
if let name = URL(string: "https://itunes.apple.com/us/app/myapp/idxxxxxxxx?ls=1&mt=8"), !name.absoluteString.isEmpty {
let objectsToShare = [name]
let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
self.present(activityVC, animated: true, completion: nil)
} else {
// show alert for not available
}
for sample see this
The solutions here are all good but it's worth considering implementing the UIActivityItemSource protocol and LinkPresentation framework.
My solution achieves the following:
App icon and title showing at the top of the UIActivityViewController
Direct link to App Store for AirDrop ActivityType
Custom text for messages and emails, including an opportunity to add a link to the app on Google Play if required
Subject for emails
Doesn't use the LPMetaDataProvider fetch request (as described in this WWDC 2019 262 video) so faster to load
0. Init the UIActivityViewController:
Set the items to self:
let activityVC = UIActivityViewController(activityItems: [self], applicationActivities: nil)
Exclude certain ActivityTypes which don't apply:
activityVC.excludedActivityTypes = [.addToReadingList, .assignToContact, .markupAsPDF, .openInIBooks, .saveToCameraRoll]
For iPad set the popoverPresentationController.sourceView or .barButtonItem (this is ignored on iPhone):
activityVC.popoverPresentationController?.sourceView = myButton
Present it:
present(activityVC, animated: true, completion: nil)
1. Implement the required UIActivityItemSource methods
https://developer.apple.com/documentation/uikit/uiactivityitemsource
You must implement the placeholder method which according to the docs:
Placeholder objects do not have to contain any real data but should be configured as closely as possible to the actual data object you intend to provide.
func activityViewControllerPlaceholderItem(_ activityViewController: UIActivityViewController) -> Any {
return ""
}
And the actual data, returning a link to the app for AirDrop and text for everything else:
func activityViewController(_ activityViewController: UIActivityViewController, itemForActivityType activityType: UIActivity.ActivityType?) -> Any? {
if activityType == .airDrop {
return URL(string: "APP_STORE_URL")!
}
return "Check out the APP_NAME on the App Store: APP_STORE_URL or on the Google Play Store: PLAY_STORE_URL"
}
2. Implement the subject method
From the docs:
For activities that support a subject field, returns the subject for the item.
func activityViewController(_ activityViewController: UIActivityViewController, subjectForActivityType activityType: UIActivity.ActivityType?) -> String {
return "EMAIL_SUBJECT" // e.g. App name
}
3. Implement the LPLinkMetaData method
From the docs:
Returns metadata to display in the preview header of the share sheet.
#available(iOS 13.0, *)
func activityViewControllerLinkMetadata(_ activityViewController: UIActivityViewController) -> LPLinkMetadata? {
let metadata = LPLinkMetadata()
metadata.title = "APP_NAME"
return metadata
}
iPhone solution for Swift 5+
let url = URL(string: "https://apps.apple.com/us/app/id1535629801")!
let vc = UIActivityViewController(activityItems: [url], applicationActivities: nil)
present(vc, animated: true)

Resources