7

I am using UIActivityViewController 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 a mail Subject in UIActivityViewController? The accepted solution is using UIActivityItemSource with this following API activityViewController:subjectForActivityType:. However, our code doesn't conform to UIActivityItemSource because we are using UIActivityItemProvider.

UIActivityItemSource

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 UIActivityItemProvider instead of UIActivityItemSource?

Community
  • 1
  • 1
Yuchen
  • 24,092
  • 18
  • 133
  • 193
  • 1
    Look this question: http://stackoverflow.com/questions/20581389/how-do-i-use-uiactivityitemprovider-to-send-an-email-with-attachment-with-uiacti – weso Jan 21 '15 at 14:53
  • This is kind of cool. I didn't know that we can still use UIActivityItemProvider without even explicitly adding it to UIActivityItemProvider. Thanks man. This sounds like the correct answer. – Yuchen Jan 21 '15 at 16:32

3 Answers3

7

Define your custom item provider:

@interface CustomProvider : UIActivityItemProvider
@end

Add to your implementation:

@implementation CustomProvider

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

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

@end

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

Yuchen
  • 24,092
  • 18
  • 133
  • 193
5

Just add this line after you instantiate your UIActivityViewController:

[activityController setValue:@"Your email Subject" forKey:@"subject"];

I am using it like this:

- (void)share {
    NSArray *activityItems;

    NSString *texttoshare = [NSString stringWithFormat:@"Hey bro! check this info.\n%@\n%@", self.infoBean.title, self.infoBean.desc];
    UIImage *imagetoshare = imageView.image;//this is your image to share

    if (imagetoshare != nil) {
    activityItems = @[imagetoshare, texttoshare];
    } else {
    activityItems = @[texttoshare];
    }
    NSArray *exTypes = @[UIActivityTypeAssignToContact, UIActivityTypePrint, UIActivityTypeCopyToPasteboard, UIActivityTypeSaveToCameraRoll];


    UIActivityViewController *activityController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
    activityController.excludedActivityTypes = exTypes;


    [activityController setValue:@"Your email Subject" forKey:@"subject"];

    [self presentViewController:activityController animated:YES completion:nil];
}
sazz
  • 2,727
  • 3
  • 19
  • 30
  • 1
    Thanks, this is one possible answer. However, I don't quite like using undocumented api. I will provide a answer which is a more elegant one based on the comments. – Yuchen Jan 21 '15 at 16:34
1

UIActivityItemProvider implements the UIActivityItemSource protocol. It's right there in the header.

@interface UIActivityItemProvider : NSOperation <UIActivityItemSource>

so you can simply use the method - (NSString *)activityViewController:(UIActivityViewController *)activityViewController subjectForActivityType:(NSString *)activityType to return the subject in your UIActivityItemProvider subclass.

orkoden
  • 16,170
  • 3
  • 55
  • 49