Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show thumbnail for url attachment on UIActivityViewController

When you a share a url with the UIActivityViewController, it just shows the Safari compass. I get that this is supposed to show the user that they are sharing a link, but is it possible to get this to show a thumbnail/image instead? I realize it does this if I try and share an image, but I wanted to just share a link here.

like image 947
Luke Avatar asked May 06 '13 19:05

Luke


1 Answers

Instead of sharing an NSURL directly, you can share an object that implements the UIActivityItemSource protocol. This can just be an object that wraps the NSURL and returns it when the activityViewController:itemForActivityType: method is called.

You can then implement the optional method activityViewController:thumbnailImageForActivityType:suggestedSize: to provide a thumbnail for the share dialog.

For example:

@interface URLWrapper:UIActivityItemProvider

@property (nonatomic, strong) NSURL *url;

@end

@implementation URLWrapper

+ (instancetype)activityItemSourceUrlWithUrl:(NSURL *)url {
    URLWrapper *wrapper = [[self alloc] initWithPlaceholderItem:@""];
    wrapper.url = url;

    return wrapper;
}

- (id)activityViewControllerPlaceholderItem:(UIActivityViewController *)activityViewController {
    return [self activityViewController:activityViewController itemForActivityType:nil];
}

- (id)activityViewController:(UIActivityViewController *)activityViewController itemForActivityType:(NSString *)activityType {
    return self.url;
}

- (UIImage *)activityViewController:(UIActivityViewController *)activityViewController thumbnailImageForActivityType:(NSString *)activityType suggestedSize:(CGSize)size {
    UIImage* thumbnail; // get image from somewhere!
    return thumbnail;
}

@end

You can then share wrapped URLs just as you would share normal instances of NSURL.

like image 171
AHM Avatar answered Sep 30 '22 17:09

AHM