UIActivityItemSource Protocole set complex object

2019-02-08 03:25发布

问题:

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

回答1:

After spending a significant amount of time trying to figure this one out, it seems it isn't possible to pass it an NSArray of items. So I extended UIActivityViewController to make it possible.

RDActivityViewController



回答2:

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.