Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initial text and paperclipped-URL for action in UIActivityViewController & UIActivityItemSource?

Finally been making it through Apple's (rather dismal) documentation on the new UIActivityViewController class and the UIActivityItemSource protocol, and I'm trying to send different data sets to different actions called from the activity view. To simplify things, I'm looking at two things.

  1. A Facebook posting action, which should say "Check this out!" and also attach a URL to the post (with that cute little paperclip).
  2. A Twitter posting action, which should say "Check this out, with #hashtag!" and also attach that same URL (with the same paperclip).

Here's the code I've got implemented right now.

- (id)activityViewController:(UIActivityViewController *)activityViewController itemForActivityType:(NSString *)activityType {
    if ([activityType isEqualToString:UIActivityTypePostToFacebook]) {
        return @"Check this out!";
    } else if ([activityType isEqualToString:UIActivityTypePostToTwitter]) {
        return @"Check this out, with #hashtag!";
    }

    return @"";
}

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

And then when I set up this activity view controller (it's in the same class), this is what I do.

UIActivityViewController *activityView = [[UIActivityViewController alloc] initWithActivityItems:@[self] applicationActivities:nil];
[self presentViewController:activityView animated:YES completion:nil];

My dilemma is how to attach that NSURL object. It's relatively easy when calling the iOS 6 SL-class posting modals; you just call the individual methods to attach a URL or an image. How would I go about doing this here?

I'll note that instead of returning NSString objects from -activityViewController:itemForActivityType, if I return just NSURL objects, they show up with that paperclip, with no body text in the post. If I return an array of those two items, nothing shows up at all.

like image 833
Ben Kreeger Avatar asked Oct 09 '12 21:10

Ben Kreeger


1 Answers

Evidently it was as simple as this: passing in an array to the first argument of UIActivityViewController's init call, with each item in the array handling a different data type that will end up in the compose screen. self handles the text, and the second object (the NSURL) attaches the URL.

NSArray *items = @[self, [NSURL URLWithString:@"http://this-is-a-url.com"]];
UIActivityViewController *activityView = [[UIActivityViewController alloc]  initWithActivityItems:items applicationActivities:nil];
[self presentViewController:activityView animated:YES completion:nil];

Really wish there was more on this, but here it is.

like image 118
Ben Kreeger Avatar answered Oct 21 '22 05:10

Ben Kreeger