Skip to content

Instantly share code, notes, and snippets.

@w-i-n-s
Forked from shu223/CustomActivity.swift
Last active December 18, 2017 19:04
Show Gist options
  • Save w-i-n-s/860468feab760180cca1e2213578a213 to your computer and use it in GitHub Desktop.
Save w-i-n-s/860468feab760180cca1e2213578a213 to your computer and use it in GitHub Desktop.
Custom UIActivity in Swift 3
import UIKit
class CustomActivity: UIActivity {
var actName = ""
var actImage: UIImage?
var customActionWhenTapped:( ()-> Void)!
init(title: String, image: UIImage, performAction: @escaping (() -> ()) ) {
self.actName = title
self.actImage = image
self.customActionWhenTapped = performAction
super.init()
}
override class var activityCategory: UIActivityCategory {
return .action
}
override var activityType: UIActivityType? {
guard let bundleId = Bundle.main.bundleIdentifier else {return nil}
return UIActivityType(rawValue: bundleId + "\(self.classForCoder)")
}
override var activityTitle: String? {
return actName
}
override var activityImage: UIImage? {
return actImage
}
override func canPerform(withActivityItems activityItems: [Any]) -> Bool {
return true
}
override func prepare(withActivityItems activityItems: [Any]) {
//
}
override func perform() {
customActionWhenTapped()
activityDidFinish(true)
}
}
@w-i-n-s
Copy link
Author

w-i-n-s commented Dec 18, 2017

Now you can init custom UIActivity with name/image/closure, from any place

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment