I have created a share extension for my app through which users will be able to create a post with images. It works perfectly well when any image from "Photos" is selected and shared but when a screenshot is taken and directly shared, the share extension is not able to fetch it.
I have written the code that is used to fetch images.
-(void)fetchImages {
for (NSExtensionItem *item in self.extensionContext.inputItems) {
[item.attachments enumerateObjectsUsingBlock:^(NSItemProvider * _Nonnull itemProvider, NSUInteger idx, BOOL * _Nonnull stop) {
if ([itemProvider hasItemConformingToTypeIdentifier:(NSString *)kUTTypeImage]) {
[itemProvider loadItemForTypeIdentifier:(NSString *)kUTTypeImage options:nil completionHandler:^(NSData *image, NSError *error) {
// You will get Image data here that you can submit to server.
[self showAlertWithMessage:[error localizedDescription]];
if(image) {
[self saveImage:image atIndex:idx];
}
}];
}
}];
}
}
The above method is called after viewDidLoad. When any image is selected from "Photos" it is displayed perfectly well but when a screenshot is shared immediately after being taken, I get the error "could not coerce an item to class nsdata". Any help on this would me much appreciated. Thanks.