Recently I implemented ShareExtension in my ios app built using react-native.
coming straight to the point.
added below code to info.plist of ShareExtension app to support sharing of any kind of file/attachments available
<dict>
<key>NSExtensionActivationRule</key>
<dict>
<key>NSExtensionActivationSupportsText</key>
<true/>
<key>NSExtensionActivationSupportsWebURLWithMaxCount</key>
<integer>1</integer>
<key>NSExtensionActivationSupportsImageWithMaxCount</key>
<integer>10</integer>
<key>NSExtensionActivationSupportsMovieWithMaxCount</key>
<integer>10</integer>
<key>NSExtensionActivationSupportsFileWithMaxCount</key>
<integer>10</integer>
<key>NSExtensionActivationSupportsAttachmentsWithMaxCount</key>
<integer>10</integer>
</dict>
</dict>
and now I
-
opened Photos
-
selected an image to share
-
from list of apps shown to share, I chose my app
from the below code I got the url details but could not repeat the same while sharing PDFs
NSExtensionItem *inputItem = self.extensionContext.inputItems.firstObject;
NSItemProvider *itemProvider = inputItem.attachments.firstObject;
if ([itemProvider hasItemConformingToTypeIdentifier:(NSString *)kUTTypeImage]) {
[itemProvider loadItemForTypeIdentifier:(NSString *)kUTTypeImage options:nil completionHandler:^(NSURL *url, NSError *error) {
if (url) {
NSLog(@"Shared file URL: %@", url);
[self uploadFileWithURL:url];
} else {
NSLog(@"Error loading shared file: %@", error);
}
}];
}
My trials – for sharing PDFs in the same manner,
if ([itemProvider hasItemConformingToTypeIdentifier:(NSString *)kUTTypeFileURL]) {
[itemProvider loadItemForTypeIdentifier:(NSString *)kUTTypeFileURL options:nil completionHandler:^(NSURL *url, NSError *error) {
if (error) {
NSLog(@"Error loading PDF: %@", error.localizedDescription);
} else if (url) {
NSLog(@"Shared PDF URL: %@", url);
}
}];
}
just changed kUTTypeImage –> kUTTypeFileURL
but everytime its throwing error saying
Failed to resolve item of class `NSURL` for type `public.file-url` with error: Error Domain=NSPOSIXErrorDomain Code=3 "No such process" UserInfo={NSLocalizedDescription=Cannot issue a sandbox extension for file "/Users/demouser/Library/Developer/CoreSimulator/Devices/{ID}/data/Containers/Data/Application/{SOME_ID}/Documents/PDF_NAME.pdf": No such process}
and also logging
Error loading PDF: Could not coerce an item to class NSURL
what I want ——> I just want to share the pdf file just like the image(which is working fine)
IF anybody have any prior experience, do kindly share, it will be very helpful.