UIActivityItemSource Protocole set complex object

Thomas Besnehard picture Thomas Besnehard · Oct 23, 2012 · Viewed 10.7k times · Source

I'm using iOS 6 new way to share information : UIActivityViewController. To select the shared data depending on the media (facebook, twitter or mail) my view controller implement the UIActivityItemSource Protocol as follow :

- (IBAction)onShareButton:(UIButton *)sender
{

    UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:@[self] applicationActivities:nil];

    activityViewController.excludedActivityTypes = @[UIActivityTypeMessage, UIActivityTypeAssignToContact, UIActivityTypeCopyToPasteboard, UIActivityTypeMessage, UIActivityTypePostToWeibo, UIActivityTypePrint, UIActivityTypeSaveToCameraRoll];
    [self presentViewController:activityViewController animated:YES completion:^{}];
}

#pragma mark - UIActivityItemSource Protocol

- (id)activityViewController:(UIActivityViewController *)activityViewController itemForActivityType:(NSString *)activityType {
    if ([activityType isEqualToString:UIActivityTypePostToFacebook]) {
        NSArray *items = @[@"message facebook", [NSURL URLWithString:@"http://www.myUrlFacebook.com"]];
        return items;
    } else if ([activityType isEqualToString:UIActivityTypePostToTwitter]) {
        NSArray *items = @[@"message twitter", [NSURL     URLWithString:@"http://www.myUrlTwitter.com"]];
        return items;
    } else if ([activityType isEqualToString:UIActivityTypeMail]) {
        NSArray *items = @[@"message mail", [NSURL URLWithString:@"http://www.myUrlMail.com"]];
        return items;
    }

        NSArray *items = @[@"Not a proper Activity", [NSURL URLWithString:@"http://www.myUrlMail.com"]];
    return items;
}

- (id)activityViewControllerPlaceholderItem:(UIActivityViewController *)activityViewController {
    return @"PlaceHolder";
}

When I'm returning a simple NSString for activityViewController:itemForActivityType: the string is well used by my UIActivityViewController, but I can't find a way to use an Array !

According to Apple Documentation it should be possible :

This method returns the actual data object to be acted on by an activity object Apple documentation

Does anyone ever use this UIActivityItemSource Protocol with Arrays, or is there a use full tutorial to do that ?

Note : I also got this error on the console, it may help ...

Launch Services: Registering unknown app identifier com.apple.mobilemail failed

Launch Services: Unable to find app identifier com.apple.mobilemail

Answer

Doug picture Doug · Dec 10, 2012

A single object conforming to UIactivityItemSource can only return a single piece of data for activityViewControllerPlaceholderItem:, no NSArrays.

You could overcome this by creating and passing two UIActivityItemSources in the activityItems part of the initial initWithActivityItems:. Each source can pass a placeholder value, but can return something blank on itemForActivityType so you don't actually have to use that particular type of data depending on the activity.

Or just use that cool extension mentioned in the other answer.