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.

Monday, January 19, 2015

How to use both Objective C and Swift in same Project


With the introduction of new language Swift, developers can use this language to develop the iOS project. But most of the APIs are available in Objective C. So if any developer is developing the project in Swift language and they wants to use these Objective C API in there project then they can do it.

Even Developers can create the project with mix and match of both languages Objective C and Swift.

This post will tell you how you can use both language in the application.


Use Objective C in Your Swift project-

If you want to use Swift file in your Objective C project, just create a Objective C file and Xcode will prompt you asking “Would you like to create Objective C Bridging Header?”





When you tap on Yes it will create a Bridging Header file with name "ProductName-Bridging-Header.h"

You can import your Objective C code to Swift by importing your header file in this Bridging header file.





Use Swift in Your Objective-C project-


In order to use Swift file in your Objective C project, just create a Swift file by selecting File> New > Swift.



Now Xcode will prompt you asking “Would you like to create Objective C Bridging Header?”
 You can import your Swift code to any Objective C file by using this syntax-

#import “ProjectName-Swift.h”



You can download the sample app from Github.