priore

Custom action for a specific share types

May 10th, 2018
195
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. // custom action for a specific share types
  3. //
  4.  
  5. class MyCustomShare: NSObject, UIActivityItemSource {
  6.    
  7.     static func share() {
  8.        
  9.         let vc = UIViewController.topViewController()
  10.        
  11.         let activityViewController = UIActivityViewController(activityItems: [self], applicationActivities: nil)
  12.         activityViewController.popoverPresentationController?.sourceView = vc?.view
  13.         vc?.present(activityViewController, animated: true, completion: nil)
  14.     }
  15.    
  16.     // MARK: - UIActivityItemSource Protocol
  17.  
  18.     func activityViewController(_ activityViewController: UIActivityViewController, itemForActivityType activityType: UIActivityType?) -> Any? {
  19.        
  20.         var message: String = "you custom message"
  21.        
  22.         if activityType == UIActivityType.postToFacebook {
  23.             activityViewController.dismiss(animated: false) {
  24.                
  25.                 // TODO: your custom actions
  26.                
  27.             }
  28.            
  29.             return nil
  30.         }
  31.        
  32.         return message
  33.  
  34.     }
  35.  
  36.    
  37.     func activityViewController(_ activityViewController: UIActivityViewController, subjectForActivityType activityType: UIActivityType?) -> String {
  38.        
  39.         return "your subject" // form email
  40.     }
  41.    
  42.     func activityViewController(_ activityViewController: UIActivityViewController, thumbnailImageForActivityType activityType: UIActivityType?, suggestedSize size: CGSize) -> UIImage? {
  43.        
  44.         var image: UIImage?
  45.        
  46.         if let imageUlr = "your image url", let url = URL(string: imageUlr) {
  47.             guard let data = try? Data(contentsOf: url) else {
  48.                 return nil
  49.             }
  50.            
  51.             image = UIImage(data: data)
  52.         }
  53.        
  54.         return image
  55.     }
  56.    
  57.     func activityViewControllerPlaceholderItem(_ activityViewController: UIActivityViewController) -> Any {
  58.         return ""
  59.     }
  60.    
  61. }
  62.  
  63. extension UIViewController {
  64.    
  65.     static var root: UIViewController? {
  66.         get {
  67.             return UIApplication.shared.delegate?.window??.rootViewController
  68.         }
  69.     }
  70.    
  71.     static func topViewController(from viewController: UIViewController? = UIViewController.root) -> UIViewController? {
  72.         if let tabBarViewController = viewController as? UITabBarController {
  73.             return topViewController(from: tabBarViewController.selectedViewController)
  74.         } else if let navigationController = viewController as? UINavigationController {
  75.             return topViewController(from: navigationController.visibleViewController)
  76.         } else if let presentedViewController = viewController?.presentedViewController {
  77.             return topViewController(from: presentedViewController)
  78.         } else {
  79.             return viewController
  80.         }
  81.     }
  82.  
  83.    
  84. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×