Yuchen Zhong Yuchen Zhong - 1 year ago 171
iOS Question

How to set mail subject with UIActivityItemProvider

I am using

for sharing information through email. We are able to send email with body, attachments with no problem. But how do we set the subject title for the email.

I notice this question: How to set Mail Subject in UIActivityViewController? And also twitter sharing text compression?
The accepted solution is using
with this following API activityViewController:subjectForActivityType:. However, our code doesn't conform to
because we are using


You can use this protocol in situations where you want to provide the data from one of your app’s existing objects instead of creating a separate UIActivityItemProvider object.

So the complete question is:

How do I set the email subject if I am using
instead of UIActivityItemSource?

Answer Source

Define your custom item provider:

@interface CustomProvider : UIActivityItemProvider

Add to your implementation:

@implementation CustomProvider

// Some other code ... -(id)item and etc. 

- (NSString *) activityViewController:(UIActivityViewController *)activityViewController
               subjectForActivityType:(NSString *)activityType
      return @"A dummy Title";


Notice that UIActivityItemProvider will automatically conform to UIactivityItemSource protocol. The difference is, you don't have to implement those @required API for UIactivityItemSource protocol.