Creating custom share actions in iOS with Swift


Bjarte's Blog

Creating custom share actions are relatively easy to create. Start off by creating a new swift project. Add a simple button to the storyboard and connect this to an outlet action.

Create a new class and name it FavoriteActivity and inside it create the following code:

class FavoriteActivity: UIActivity {
    override func activityType() -> String? {
        return "TestActionss.Favorite"
    }
    
    override func activityTitle() -> String? {
        return "Add to Favorites"
    }
    
    override func canPerformWithActivityItems(activityItems: [AnyObject]) -> Bool {
        NSLog("%@", __FUNCTION__)
        return true
    }
    
    override func prepareWithActivityItems(activityItems: [AnyObject]) {
        NSLog("%@", __FUNCTION__)
    }
    
    override func activityViewController() -> UIViewController? {
        NSLog("%@", __FUNCTION__)
        return nil
    }
    
    override func performActivity() {
        // Todo: handle action:
         NSLog("%@", __FUNCTION__)
        
        self.activityDidFinish(true)
    }
    
    override func activityImage() -> UIImage? {
        return UIImage(named: "favorites_action")
    }
}

You need to handle the events in this later depending on what you want to do.

Creating the UIActivityViewController

To be able to use this in…

View original post 177 more words