Last active
April 7, 2018 19:36
-
-
Save iAmrSalman/7e7b49cbee2ce07edab0d4f8b61db282 to your computer and use it in GitHub Desktop.
[Sharable] a protocol to handle sharing via UIActivityViewController #swift #protocol
This file contains hidden or 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
| 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