Implementing the UIActivityItemSource protocol
You can implement the protocol wherever you want, even your viewcontroller is fine. Just instantiate the activityViewController with initWithActivityItems:@[self]
.
The answer by JotWee helped me out.
There is no need for sub classing, UIActivityItemSource
protocol methods can be implemented in view controller where share button is implemented.
Very important to add self
in activity items array, like this (as JotWee suggested):
NSArray *activityItems = [NSArray arrayWithObjects:self, url, image, nil];
Here is my final implementation:
ViewController.h
@interface ViewController : UIViewController <UIActivityItemSource>
ViewController.m
- (void)shareBarButtonItemClick:(UIBarButtonItem *)sender
{
NSURL *url = [NSURL URLWithString:@"http://example.com"];
NSURL *imageUrl = [NSURL URLWithString:@"http://example.com/images/1.jpg"];
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageUrl]];
NSArray *activityItems = [NSArray arrayWithObjects:self, url, image, nil];
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
[self presentViewController:activityViewController animated:YES completion:nil];
}
- (id)activityViewController:(UIActivityViewController *)activityViewController itemForActivityType:(NSString *)activityType
{
return @"Summary Text";
}
- (id)activityViewControllerPlaceholderItem:(UIActivityViewController *)activityViewController
{
return @"";
}
- (NSString *)activityViewController:(UIActivityViewController *)activityViewController subjectForActivityType:(NSString *)activityType
{
return @"Subject";
}