0

I am trying to customize the content shared using the UIActivityViewController. The method suggested in some blog posts and answers on SO is to subclass UIActivityItemProvideras below. Some answers also say that the class should adopt a protocol UIActivityItemSource but I am fuzzy on what the protocol has to do with the Provider.

My code is in a view controller class which I have made accept the <UIActivityItemSource Protocol> in the .h file. I have also placed the following method in the .m file along with similar methods for message, placeholder, URL and thumbnail. However, they are not having any effect on the content shared which is the same for all the types, message, email, Facebook, etc.

What am I missing?

-(NSString *)activityViewController:(UIActivityViewController *)activityViewController subjectForActivityType:(UIActivityType)activityType
{
    if (activityType == UIActivityTypeMessage) {
        return @"My message subject";
    } else if (activityType == UIActivityTypeMail) {
        return @"My mail subject";
    }
    return @"My subject otherwise";
}

Edit:

The following code works fine to send identical content to different activities. But the above methods are evidently necessary to customize:

NSArray *activityItems = @[text,url];
        UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems: activityItems applicationActivities:nil];
        activityViewController.excludedActivityTypes = @[UIActivityTypePostToWeibo,UIActivityTypePrint,UIActivityTypeCopyToPasteboard,UIActivityTypeSaveToCameraRoll];
    [activityViewController setValue:_dare.shtodo forKey:@"Did you see this?"];

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

Thanks in advance for any suggestions on what I might be missing to get this to work.

user6631314
  • 1,751
  • 1
  • 13
  • 44
  • Have you checked [this](https://developer.apple.com/documentation/uikit/uiactivityviewcontroller)? – Ryan Feb 26 '18 at 21:41

1 Answers1

1

You need to pass an instance or instances of your class that implements UIActivityItemSource when creating a UIActivityViewController with initWithActivityItems:applicationActivities: - see the documentation here. A view controller is probably not a good choice for this, I'd instead make dedicated objects to do this. Ultimately your code could look something like:

MySourceClass *mySource = [MySourceClass new];
NSArray *activityItems = @[mySource];
UIActivityViewController *activityViewController =
    [[UIActivityViewController alloc] initWithActivityItems:activityItems
                                      applicationActivities:nil];
Luke
  • 7,110
  • 6
  • 45
  • 74
  • I get confused by new class. Does this mean I need to create a whole new class using CMD n with a mySource.h and mySource.m that subscribes to protocol and has the subjectForActivityType and similar methods? Otherwise the code I am using is similar to yours--see Edit above. It just does not distinguish by activityType. – user6631314 Feb 26 '18 at 22:31
  • Yes, that's what I'm recommending. – Luke Feb 27 '18 at 13:26
  • I have a lot of methods in the VC to do things like create an .ics file for the URL. How would I pass all this to the object file. By giving the object file properties? as in mySource.url = URL? and an URL property in the mysource.h file? – user6631314 Feb 27 '18 at 14:29
  • [mySourceClass new] giving error: Use initWithPlaceholderItem: to instantiate an instance of IDActivityItemProvider. I have put that method in the class but calling [mySourceClass initWithPlaceholderItem:@"string"] gives no class method found error – user6631314 Feb 27 '18 at 15:19