iosobjective-cemailuiactivityviewcontrollerairdrop

use UIActivityViewController to serve data with different format


In my app, I would like to share shopping list. Let's say, I would like to share the shopping list in two different ways:

  1. Through AirDrop with a schema url, which will bring up my app on another device, and the receiving app adds the shared items to its shopping list.
  2. Through email, which sends an email with the items on the shopping list as a HTML document in a table format.

So bascially there are two different strings to be shared across depend on whether the activity type is AirDrop or email. The below code does not work, in the sense that it is always the schema URL got sent, even for email:

url = [@"myapp://shoppinglist?apple=12&orange=5" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];    
UIActivityViewController *controller = [[UIActivityViewController alloc] initWithActivityItems:@[url] applicationActivities:nil];
[self presentViewController:controller animated:NO completion:nil]; 

I dug a bit, but could not find out how to send different contents based on the method of sharing.

My question is how to do what I described here with UIActivityViewController? Or whether it is even doable - whether UIActivityViewController was meant for this or has the flexibility for doing this?


Solution

  • You'd want to create and share two objects that conform to the UIActivityItemSource, where one returns the HTML and the other one the URL. Then when the delegate callback requesting the item is called you check which activity type was selected (Facebook, Mail, AirDrop, etc) and have one or the other return nil if that item doesn't apply.

    So in the case of AirDrop, only the item source for the URL will return a non-nil value. You can take a look at the airdrop sample code to get some examples of how to implement UIActivityItemSource