Return text, url and image in UIActivityItemProvider

1.3k views Asked by At

I am subclassing UIActivityItemProvider for my UIActivityViewController so that when the user selects UIActivityTypeMessage it will only show a message, but if the user selects UIActivityTypeFacebook it will show a message with an image.

This is my code and I can return only a message with UIActivityTypeMessage but for UIActivityTypeFacebook I do not know how to return a message with an Image.

- (id)item
{
    if ([self.placeholderItem isKindOfClass:[NSString class]]) {
        if ([self.activityType isEqualToString:UIActivityTypeMessage]) {
            return [NSString stringWithFormat:@"%@\n%@",self.text,self.url];
        } else {
            NSDictionary *d = [[NSDictionary alloc] initWithObjects:@[self.text,self.image] forKeys:@[@"text",@"image"]];
            return d;
        }
    }
    return self.placeholderItem;
}

Are there specific keys and values for a UIActivityItemProvider I can use to set the message, url and image?

As an update I read in UIActivity Class Reference that there are constants for a specific activity type. Not sure how to use them though. Can someone enlighten me please?

*UIActivityTypePostToFacebook The object posts the provided content to the user’s wall on Facebook.

When using this service, you can provide NSString, NSAttributedString, UIImage, ALAsset, and NSURL objects as data for the activity items. You may also specify NSURL objects whose contents use the assets-library scheme.

1

There are 1 answers

0
zeroimpl On

You will need to create 3 separate activity providers, one for each data type, and pass all of them in in the initWithActivityItems:applicationActivities: function on UIActivityViewController. The activities make use of all the providers you provide, and UIActivityViewController hides any one which doesn't support ALL of them (which can make it difficult to only include an image in some cases, for example).