Sharing with Airdrop in iOS7

In iOS 7, Apple introduced a new feature called AirDrop. With AirDrop, you can easily share data with other nearby iOS devices. AirDrop uses Bluetooth to scan for nearby devices. When a connection is established via Bluetooth, it’ll create an ad-hoc Wi-Fi network to link the two devices together. In this tutorial we will share a picture between two devices using Airdrop.

Note this tutorial can only run on newer devices, as the iOS Simulator doesn't have an Airdrop function.

Open Xcode and create a new Single View Application. For product name, use iOS7SharingAirdropTutorial and then fill out the Organization Name, Company Identifier and Class Prefix fields with your customary values. Make sure only iPhone is selected in Devices. 

Go the Storyboard and drag a Image View to the main view. Also drag a Button to the main view and give it a tiltle of "Share". The Storyboard should look like this.

For our tutorial we need something to share. Download this zip file and extract it. Add the image file to your project. Select the Image View and in the Attributes Inspector add the filename in the Image field.

Select the Assistant Editor and open ViewController.m. Ctrl and drag from the button to the @interface section and create the following Action.

In ViewController.m add the following property

@property (nonatomic,strong) NSArray *activityItems;

The activityItems array will contain a description and the image itself. Add the following lines to the end of viewDidLoad

NSString *shareString = @"This is my Development Machine.";
UIImage *shareImage = [UIImage imageNamed:@"imac.jpeg"];

self.activityItems = @[shareString,shareImage];

Next, we will implement the share method

- (IBAction)share:(UIButton *)sender {
UIActivityViewController *avController = [[UIActivityViewController alloc] initWithActivityItems:self.activityItems applicationActivities:nil];

// Exclude all activities except AirDrop.
NSArray *excludedActivities = @[UIActivityTypePostToTwitter, UIActivityTypePostToFacebook,
UIActivityTypePostToWeibo,UIActivityTypeMessage, UIActivityTypeMail,UIActivityTypePrint, UIActivityTypeCopyToPasteboard,UIActivityTypeAssignToContact, UIActivityTypeSaveToCameraRoll,UIActivityTypeAddToReadingList, UIActivityTypePostToFlickr,UIActivityTypePostToVimeo, UIActivityTypePostToTencentWeibo];
avController.excludedActivityTypes = excludedActivities;

[self presentViewController:avController animated:YES completion:nil];

We create the Activity ViewController with our activityItems array. With the excludedActivityTypes property we will exclude all unneeded activities, leaving Airdrop as the only sharing option. Finally we present the Activity View Controller. Build and Run the project, press the share button to share the image using Airdrop.

You can download the source code of the iOS7SharingAirdropTutorial at the ioscreator repository on github.