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.
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
.
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