Thursday, January 29, 2015

How to open Share and Action Extension from your application?


With the introduction of Extension in iOS 8, many applications is available with Share and Action Extension and if we want to share content and perform some action from our application to those app then we can open the extensions of those application using UIActivityViewController.

UIActivityViewController is a standard view controller which can offers System specific services such as copying items to the pasteboard, posting content to social media sites, sending items via email or SMS from your application.

Below is the code to initiate UIActivityViewController.

let text = "Text to share"
        let url : AnyObject! = NSURL(string: "https://www.google.co.in")
        let image : AnyObject = UIImage(named:"download.jpeg")!
        let activityItems : [AnyObject] = [text, url, image]
        
        let activityViewController : UIActivityViewController = UIActivityViewController(activityItems: activityItems, applicationActivities: nil)
        self.presentViewController(activityViewController, animated: true, completion: nil)


Here, we have created array of AcitivityItems(image, text, url) and passed it to the UIActivityViewController Constructor.

This will show all the Share and Action Extension supported by the system or application which are installed in your devices.




Note- User can manually enable and disable the application by tapping the More shown in the Share and Action Activity section. User can also reorder this activities.



     
There are 2 types of activities- Share and Action. Following are the activities for which system has built in supports-

 UIActivityTypePostToFacebook;
 UIActivityTypePostToTwitter;
 UIActivityTypePostToWeibo;
 UIActivityTypeMessage;
 UIActivityTypeMail;
 UIActivityTypePrint;
 UIActivityTypeCopyToPasteboard;
 UIActivityTypeAssignToContact;
 UIActivityTypeSaveToCameraRoll;
 UIActivityTypeAddToReadingList;
 UIActivityTypePostToFlickr;
 UIActivityTypePostToVimeo;
 UIActivityTypePostToTencentWeibo;
 UIActivityTypeAirDrop;

By Default, UIActivityViewController will show all the available activities. If you want to exclude any of the activity from your application then you can do this by passing the array of activities which you want to exclude to excludedActivityTypes property of UIActivityViewController.

        activityViewController.excludedActivityTypes = [UIActivityTypeAirDrop];

Here, we have excluded AirDrop to be shown in our application.




You can also check when the activity has been completed by calling completionWithItemsHandler.

 activityViewController.completionWithItemsHandler = {
            (activity, success, items, error) in
            println("Activity: \(activity) Success: \(success) Items: \(items) Error: \(error)")
        }

This is the complete method which you can use in your application and connect the action with the Button.
  
    @IBAction func shareAction(sender: AnyObject) {
        
        let text = "Text to share"
        let url : AnyObject! = NSURL(string: "https://www.google.co.in")
        let image : AnyObject = UIImage(named:"download.jpeg")!
        let activityItems : [AnyObject] = [text, url, image]
        
        let activityViewController : UIActivityViewController = UIActivityViewController(activityItems: activityItems, applicationActivities: nil)
        activityViewController.excludedActivityTypes = [UIActivityTypeAirDrop];
        self.presentViewController(activityViewController, animated: true, completion: nil)
        
        activityViewController.completionWithItemsHandler = {
            (activity, success, items, error) in
            println("Activity: \(activity) Success: \(success) Items: \(items) Error: \(error)")
        }
        
    }


Download the complete Project from here.

No comments:

Post a Comment