alinz / react-native-share-extension

react-native as an engine to drive share extension
MIT License
763 stars 398 forks source link

Share media files with main application #27

Open svenadlung opened 7 years ago

svenadlung commented 7 years ago

Thank you for this great module. After some problems in the beginning I got it running very well!

Is there a way to share a media file (photo/video) with the main application? I set app groups for the extension and the application. Within the extension I added react-native-fs to get access to the filesystem. But I have no idea how to save the image to the shared folder and open it within my main application.

A workaround could be using your RNSKBucket and save the image as a base64 string, saving it while opening the app by writing it to the cache folder and clearing the bucket afterwards. But this seems a little hacky.

I am not really into Objective-C programming and I was wondering how to achieve this with react native, any other ideas?

alinz commented 7 years ago

@svenadlung thanks for using this project, I would like to ask you to send me PR for updating doc so other developers won't have the early problems as you faced. That would be great.

about your question, I think it would be better if you can save the location of the media content and then, the main app can load the content and do other stuff with it. Remember one thing, share extension should be a very simple and light app so user can trigger the action and let it go.

svenadlung commented 7 years ago

@alinz Thanks for your quick response.

Well. this is an idea I already thought of. But what happens if the media file is deleted meanwhile. I want to store it as an copy within my application. So if it gets shared (just by saving the path) but deleted afterwards, no copy of the file is available within the application. Do you understand my point?

markdaws commented 7 years ago

I have some code that would do what you want - I had to update this extension to save the exported share item to a shared container because currently the share extension assumes that all images when shared will result in a NSURL, which isn't the case when you share items, it can return from loadItemForTypeIdentifier as a NSURL, UIImage, NSData for the last two, UIImage, NSData* I had to store these in a shared app container to create a URL I could then access later to upload.

The code is here in a fork of this project: https://github.com/markdaws/react-native-share-extension/commit/6dee20eafff488650d35b23a3708d04a8390da4a see how I write the data to a shared container (you would have to add code to write the contents returned by NSURL to it as well)

At the moment I delete all these files when the extension is closed, since I am only using them temporarily for upload, but you could obviously just keep them around, once the file is shared to the app container you can access if from the container app.

@alinz - I would be happy to try to get a pull request for these changes, since currently the share extension code doesn't work if you try to use it on apps like Notes and maps that export images not as NSUrl but as UIImage or NSData, I sent you a pull request a few days ago to support multiple attachments, that this code is based on: https://github.com/alinz/react-native-share-extension/pull/32/files if you think you will actually integrate these I can spend some more cycles on it. The only caveat being it is iOS only, I don't have android dev experience and don't have the cycles to look in to that.

npomfret commented 7 years ago

When I share images it always results in a file url being shared. Under what circumstances would I get an NSData object or a UIImage being shared?

markdaws commented 7 years ago

@npomfret - hi, based on testing of using this to share from various apps, apps seem to be able to share the data any way they want. If you share an image from Photos, it will send you a file URL, but if you go to the notes app, create a note and draw some content then share, it shares that image as NSData with public.image type, then there were others (I forget) where I got a UIImage reference, so best to test all the apps you want to support to see what they send back.

adambmc commented 7 years ago

@alinz- hi, I tried to implement your approach – saving the URL of the media content to the App Group, and then the main app used the share URL to upload the media content.

But when I try to upload the media content from the main app I getting an access denied exception:" Error Domain=NSCocoaErrorDomain Code=257 "The file “Documents” couldn’t be opened because you don’t have permission to view it." UserInfo={NSFilePath=/private/var/mobile/Library/Mobile Documents/com~apple~CloudDocs/2B9FF3E2-AB2E-4872-9622-FB7628B68693.JPG...."

Also I tried to call the function: startAccessingSecurityScopedResource() before uploading , but I keep getting the access denied exception.

Any thoughts?

AbdelhalimAhmed commented 6 years ago

@adambmc which plugin are you use to upload content in main app?

adambmc commented 6 years ago

@alinz- hi, thank you for the response. i managed to fix the problem by writing the file to a temporary folder and then sharing the path with the main App using App Group. Please close this issue.

Thanks.

isaachinman commented 6 years ago

@markdaws Hi, and thanks for this super helpful work! Almost a year has passed from your comments and I am searching for a cross-platform way to accept multiple-image sharing from other apps (not from a browser). Did you make any progress on Android support in your case?