Skip to content

Instantly share code, notes, and snippets.

@iAmrSalman
Last active April 7, 2018 19:36
Show Gist options
  • Select an option

  • Save iAmrSalman/7e7b49cbee2ce07edab0d4f8b61db282 to your computer and use it in GitHub Desktop.

Select an option

Save iAmrSalman/7e7b49cbee2ce07edab0d4f8b61db282 to your computer and use it in GitHub Desktop.
[Sharable] a protocol to handle sharing via UIActivityViewController #swift #protocol
import UIKit
protocol Sharable {
func share(_ str: String, source: UIView?, completionHandler: ((_ completed: Bool) -> Void)?)
}
extension Sharable where Self: UIViewController {
func share(_ str: String, source: UIView? = nil, completionHandler: ((_ completed: Bool) -> Void)? = nil) {
let activityController = UIActivityViewController.init(activityItems: [str], applicationActivities: nil)
activityController.popoverPresentationController?.sourceView = source ?? self.view
activityController.completionWithItemsHandler = {(activityType: UIActivityType?, completed: Bool, returnedItems: [Any]?, error: Error?) in
completionHandler?(completed)
}
self.present(activityController, animated: true, completion: nil)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment