b2cloud

19th September 2013

Custom AirDrop action in iOS7

Guides | Tutorial By 3 years ago

In iOS7 the UIActivityViewController now supports AirDrop to share files wirelessly between two devices. Because the share tray can support a lot of different share types you may not want your users sharing the same thing with AirDrop that they can share via email or Facebook.

Firstly, in case you haven’t used theĀ UIActivityViewController before, here’s how to bring it up with a regular item. I will just share a url as an example.

NSURL* url = [NSURL URLWithString:@"http://b2cloud.com.au"];
UIActivityViewController* activityViewController = [[UIActivityViewController alloc] initWithActivityItems:@[url] applicationActivities:nil];
[self.window.rootViewController presentViewController:activityViewController animated:YES completion:nil];

The share tray can share the following types: NSURL, NSString, UIImage, and NSData providing it’s in the right format.

To customize what will be shared, instead of one of these three types, you must create a subclass and implement theĀ UIActivityItemSource protocol.

To start you off, here’s a class stub.

@interface CustomActivityItem : NSObject 

@end

@implementation CustomActivityItem

//	Code here...

@end

...

CustomActivityItem* item = [[CustomActivityItem alloc] init];
UIActivityViewController* activityViewController = [[UIActivityViewController alloc] initWithActivityItems:@[item] applicationActivities:nil];
[self.window.rootViewController presentViewController:activityViewController animated:YES completion:nil];

Now implement the placeholder method. The placeholder doesn’t need to be the final object, it just lets iOS pick which share types are appropriate to show based on the type of item you’re sharing. You want to try and keep this the same class type as your final item.

To keep things simple, I will allocate the url I want to share in the init method and return it as the placeholder.

@interface CustomActivityItem : NSObject 
{
	NSURL* url;
}

@end

@implementation CustomActivityItem

- (id) init
{
	self = [super init];

	if(self != nil)
	{
		url = [NSURL URLWithString:@"http://b2cloud.com.au"];
	}

	return self;
}

- (id)activityViewControllerPlaceholderItem:(UIActivityViewController *)activityViewController
{
	return url;
}

//	Code here...

@end

And now the final method. This gets called whenever the user actually chooses a share type to share with. The activityType parameter lets you know which share option the user has picked from the tray.

- (id)activityViewController:(UIActivityViewController *)activityViewController itemForActivityType:(NSString *)activityType
{
	if([activityType isEqualToString:UIActivityTypeAirDrop])
	{
		return [NSURL URLWithString:@"http://apple.com"];
	}
	else
	{
		return url;
	}
}

Run the code. Whenever AirDrop is picked, the receiving user will be taken to the Apple website, otherwise to the b2cloud website.

You can even return a url with a custom scheme, to open another app, or even the same app but on the other user’s phone.

Here’s the final code.

@interface CustomActivityItem : NSObject  
{
	NSURL* url;
}

@end

@implementation CustomActivityItem

- (id) init
{
	self = [super init];

	if(self != nil)
	{
		url = [NSURL URLWithString:@"http://b2cloud.com.au"];
	}

	return self;
}

- (id)activityViewControllerPlaceholderItem:(UIActivityViewController *)activityViewController
{
	return url;
}

- (id)activityViewController:(UIActivityViewController *)activityViewController itemForActivityType:(NSString *)activityType
{
	if([activityType isEqualToString:UIActivityTypeAirDrop])
	{
		return [NSURL URLWithString:@"http://apple.com"];
	}
	else
	{
		return url;
	}
}

@end

...

CustomActivityItem* item = [[CustomActivityItem alloc] init];
UIActivityViewController* activityViewController = [[UIActivityViewController alloc] initWithActivityItems:@[item] applicationActivities:nil];
[self.window.rootViewController presentViewController:activityViewController animated:YES completion:nil];
  • gordonillan

    Thanks for this article. When would one use NSData as the placeholder item type and what should be its format for mixing in different choices of content (url, text, image, …) for different activity types?

    • Tom

      Often you want to keep the placeholder item’s type the same as the final type you plan to return.

      If you want to return a different type depending on the activity chosen then return an NSURL as the placeholder (I think it’s the most widely supported) and then return a UIImage, NSString or NSData in the -itemForActivityType: method. Obviously you will want to make sure the activity supports whatever type you plan to return, otherwise I think the share tray will just close without doing anything.

      As for returning NSData, the type of the data inside is considered for the supported activities shown to the user.

Recommended Posts

WWDC 2014 Predictions

Post by 3 years ago

WWDC is just one week away. I am attending this year and wanted to give my thoughts on what I predict will be announced. If you saw the WWDC app for this year, it has

Got an idea?

We help entrepreneurs, organizations and established brands from around
the country bring ideas to life. We would love to hear from you!