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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With