How to Implement "Share Button" in Swift

How to implement share button in Swift

Swift 5:

    // Setting description
let firstActivityItem = "Description you want.."

// Setting url
let secondActivityItem : NSURL = NSURL(string: "http://your-url.com/")!

// If you want to use an image
let image : UIImage = UIImage(named: "your-image-name")!
let activityViewController : UIActivityViewController = UIActivityViewController(
activityItems: [firstActivityItem, secondActivityItem, image], applicationActivities: nil)

// This lines is for the popover you need to show in iPad
activityViewController.popoverPresentationController?.sourceView = (sender as! UIButton)

// This line remove the arrow of the popover to show in iPad
activityViewController.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection.down
activityViewController.popoverPresentationController?.sourceRect = CGRect(x: 150, y: 150, width: 0, height: 0)

// Pre-configuring activity items
activityViewController.activityItemsConfiguration = [
UIActivity.ActivityType.message
] as? UIActivityItemsConfigurationReading

// Anything you want to exclude
activityViewController.excludedActivityTypes = [
UIActivity.ActivityType.postToWeibo,
UIActivity.ActivityType.print,
UIActivity.ActivityType.assignToContact,
UIActivity.ActivityType.saveToCameraRoll,
UIActivity.ActivityType.addToReadingList,
UIActivity.ActivityType.postToFlickr,
UIActivity.ActivityType.postToVimeo,
UIActivity.ActivityType.postToTencentWeibo,
UIActivity.ActivityType.postToFacebook
]

activityViewController.isModalInPresentation = true
self.present(activityViewController, animated: true, completion: nil)

Add a share button - Swift

This code will bring up an activity view and will let you share whatever you want with it.

if var top = scene?.view?.window?.rootViewController {
while let presentedViewController = top.presentedViewController {
top = presentedViewController
}
let activityVC = UIActivityViewController(activityItems: ["This is an array of items that will be shared. Including Images, Numbers, and text (like this)"], applicationActivities: nil)
activityVC.popoverPresentationController?.sourceView = view
top.present(activityVC, animated: true, completion: nil)
}

Of course, it will need to be called in the touchesBegan method or a function. To share something, you will need to put it into the array that is in activityVC.

This isn't a share button, this is just the code that is used to share something to another app. You need to put it into a function or in touchesBegan and call it separately whenever you want to share something.

Basic example for sharing text or image with UIActivityViewController in Swift

UIActivityViewController Example Project

Set up your storyboard with two buttons and hook them up to your view controller (see code below).

Sample Image

Add an image to your Assets.xcassets. I called mine "lion".

Sample Image

Code

import UIKit
class ViewController: UIViewController {

// share text
@IBAction func shareTextButton(_ sender: UIButton) {

// text to share
let text = "This is some text that I want to share."

// set up activity view controller
let textToShare = [ text ]
let activityViewController = UIActivityViewController(activityItems: textToShare, applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = self.view // so that iPads won't crash

// exclude some activity types from the list (optional)
activityViewController.excludedActivityTypes = [ UIActivity.ActivityType.airDrop, UIActivity.ActivityType.postToFacebook ]

// present the view controller
self.present(activityViewController, animated: true, completion: nil)

}

// share image
@IBAction func shareImageButton(_ sender: UIButton) {

// image to share
let image = UIImage(named: "Image")

// set up activity view controller
let imageToShare = [ image! ]
let activityViewController = UIActivityViewController(activityItems: imageToShare, applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = self.view // so that iPads won't crash

// exclude some activity types from the list (optional)
activityViewController.excludedActivityTypes = [ UIActivity.ActivityType.airDrop, UIActivity.ActivityType.postToFacebook ]

// present the view controller
self.present(activityViewController, animated: true, completion: nil)
}

}

Result

Clicking "Share some text" gives result on the left and clicking "Share an image" gives the result on the right.

Sample Image

Notes

  • I retested this with iOS 11 and Swift 4. I had to run it a couple times in the simulator before it worked because it was timing out. This may be because my computer is slow.
  • If you wish to hide some of these choices, you can do that with excludedActivityTypes as shown in the code above.
  • Not including the popoverPresentationController?.sourceView line will cause your app to crash when run on an iPad.
  • This does not allow you to share text or images to other apps. You probably want UIDocumentInteractionController for that.

See also

  • Add sharing to your Swift app via UIActivityViewController
  • UIActivity​View​Controller from NSHipster
  • UIActivityViewController documentation
  • Share extension documentation
  • comparison with UIDocumentInteractionController

How do I create a standard iOS Share button?

The standard Share button is a UIBarButtonItem (so it can only go on a navigation bar or toolbar). You need to create a “system item”; specifically, an “action item”. The “action” bar button item is the Share button.

Share app link to by ActivityViewController iOS swift?

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

Implement share button in TableViewCell with Firebase data

Get the indexPath of the cell using the button location and then extract the particular data from the model object.

Edit:

You can share the image directly along with text in UIActivityViewController, for that you need to download the image before sharing if its in url else you can share it directly by keep it in an array.

@IBAction func tapShareButton(_ sender: UIButton) {

//Get the button position in the tableView
let buttonPosition = sender.convert(CGPoint.zero, to: self.tableView)

//Find the indexPath of tableView from the button position
if let indexPath = self.tableView.indexPathForRow(at:buttonPosition) {

//Extract release from data at particular indexPath
let newRelease = releasesData[indexPath.row]

//Shoename should be the releasename
let firstActivityItem = newRelease.releasename///"Find out everywhere the *shoename* is available to purchase"

let secondActivityItem : NSURL = NSURL(string: "http//:urlyouwant")!
// Image should be the releaseimage
let image : UIImage = newRelease.releaseimage

let activityViewController : UIActivityViewController = UIActivityViewController(
activityItems: [firstActivityItem, secondActivityItem, image], applicationActivities: nil)

activityViewController.popoverPresentationController?.sourceView = (sender )
activityViewController.popoverPresentationController?.sourceRect = CGRect(x: 150, y: 150, width: 0, height: 0)

// Anything you want to exclude
activityViewController.excludedActivityTypes = [
UIActivity.ActivityType.postToWeibo,
UIActivity.ActivityType.print,
UIActivity.ActivityType.assignToContact,
UIActivity.ActivityType.saveToCameraRoll,
UIActivity.ActivityType.addToReadingList,
UIActivity.ActivityType.postToFlickr,
UIActivity.ActivityType.postToVimeo,
UIActivity.ActivityType.postToTencentWeibo
]

self.present(activityViewController, animated: true, completion: nil)
}
}

GameScene Share Button

I ran into this same issue two days ago. I fixed it using this stackoverflow answer

I kept getting the same error and it was because rootViewController was not the current top viewController in the hierarchy. It was trying to use my initial viewController when I wanted the viewController in control of the current SKScene.

Hope this helps

    func shareGame() {
if var top = scene?.view?.window?.rootViewController {
while let presentedViewController = top.presentedViewController {
top = presentedViewController
}
let activityVC = UIActivityViewController(activityItems: ["I am playing Crashbox! Check it out."], applicationActivities: nil)
activityVC.popoverPresentationController?.sourceView = view
top.present(activityVC, animated: true, completion: nil)
}
}


Related Topics



Leave a reply



Submit