3

UIActivityItemSources, it seems, can only return one kind of placeholder item? This seems strange, because I have a UIActivityItemSource that could return a string, an NSData object, or an image depending upon the activity it's given.

Is there really no way to return more than one kind of placeholder? (NSArrays don't seem to work.)

(I could imagine a solution where I instantiate a bunch of UIActivityItemProvider instances, each supporting the different datatypes mentioned above. But that seems like a lot more work than should be necessary...?)

sha
  • 17,824
  • 5
  • 63
  • 98
Greg Maletic
  • 6,225
  • 8
  • 54
  • 73
  • There should be something simpler, but until Apple builds it I used this: https://stackoverflow.com/a/70866445/611879 – budiDino Jan 26 '22 at 16:05

1 Answers1

2

If you add a trace inside your itemForActivityType function you will see that this function will be called multiple times. One for each activity available for share.

For example - if I want to provide different text for Twitter and mail/sms sharing I would have something like this:

- (id) activityViewController: (UIActivityViewController*) activityViewController itemForActivityType: (NSString*) activityType {

    if (activityType == UIActivityTypePostToTwitter) {
        return @"Sharing by Twitter";
    }
    else
        return @"Other kind of sharing";

}

UPDATE:

If you want to provide different types of data to share (say text and images) - you need to wrote your placeholder function in a way so it returns two different kind of object when called multiple times.

- (id) activityViewControllerPlaceholderItem: (UIActivityViewController*) activityViewController {
    static int step = 0;

    if (step == 0) {
        step = 1;
        return @"text";
    }
    else if (step == 1) {
        step = 2;
        return [UIImage imageNamed: @"image"];
    }
}
sha
  • 17,824
  • 5
  • 63
  • 98
  • I'm not talking about the method you mention; I'm talking about -[UIActivityItemSource activityViewControllerPlaceholderItem:]. This is the method only seems to be called once, and I apparently have no way of switching on activityType. Thanks. – Greg Maletic Dec 20 '13 at 02:56
  • Actually in my tests `activityViewControllerPlaceholderItem` gets called multiple times. But what exactly are you trying to achieve? – sha Dec 20 '13 at 03:14
  • My UIActivityItemSource returns different kinds of objects depending upon the activityType (data, image, text). But there's no activityType parameter passed into -[UIActivityItemSource activityViewControllerPlaceholderItem:], so how can I express that I return different kinds of data? – Greg Maletic Dec 20 '13 at 03:25
  • Simply return Text object in placeholder function and then return different kind of data in `itemForActivity`. – sha Dec 20 '13 at 03:26
  • 1
    But if I return a text object in the placeholder, then I just get text-related actions in the ActivityViewController. I want text actions (Twitter, email) *and* image actions (Save to Camera Roll), among others. And that would seem to require me being able to return a string *and* an image *and* a data object via placeholder, switching on the activityType. But there's no activityType parameter. What am I not getting here? Thanks. – Greg Maletic Dec 20 '13 at 03:28
  • Thank you for sharing that. Wow...I'm, uh, astonished it works that way. 1) Is there a reason the API is like that? Because returning an NSArray from that method would be a whole lot nicer. 2) When do you reset your static to 0? Because if you don't, if someone tries to share this object multiple times, the "step" variable just goes up and up. (And the situation is compounded if there are multiple instances of this class.) I mean, I can think of ways to maybe pull it off with instance vars...but wow, that seems like a mess. – Greg Maletic Dec 20 '13 at 03:42
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/43560/discussion-between-greg-maletic-and-sha) – Greg Maletic Dec 20 '13 at 03:44
  • 1
    It's likely they will change this implementation in the future, but looks like for right now - it's the only way. – sha Dec 20 '13 at 15:05