iOS File Sharing Between Apps - Activity View

Hi all,

I recently got Android to receive shared documents from other Apps (see: https://forums.coronalabs.com/topic/74107-android-file-sharing-between-apps-content-stream/ ) which was a bit of a task.

Now I’m looking to do the same for iOS.

However it’s looking more and more unlikely with Corona.

From what I can tell, I need to create an Activity View (see https://developer.apple.com/design/human-interface-guidelines/ios/views/activity-views/ ) which will allow other Apps to send (read: Save) a file to my App.

However from everything I’ve read in the forums, albeit most posts are very old, this is not possible with Corona natively.

Has anyone managed to put something similar together, or have any advice as to where to begin?

If I can get both Android and iOS working, I’ll certainly look to providing a free plugin so in future we can do this within a few lines of code (hopefully).

At the very least I’d like something that will allow files to be sent from iCloud drive to my app manually (select file, click “Share” and then choose my app to share to. Obviously if I can get this to work from all apps for specific mime types, then this would be much better.

Many thanks,

Paul

We have an “Activity” plugin, but I’m not sure if it will do what you want. It brings up the activity view which is great for sharing out from your app, but that plugin isn’t designed for bringing data in. You need to build an “App Extension”

https://stackoverflow.com/questions/28248246/how-to-make-your-app-available-in-uiactivityviewcontroller-listed-activities

Corona’s simulator builds can’t do this. Intead you would probably need to work out something with Corona native builds since it involves adding an extension to your app. I’m not sure what’s involved, but you would need to go this route if it works.

Rob

Thanks Rob,

That’s more or less what I read / thought. While I’m up for the challenge (which won’t be particularly easy for someone new to Corona) I am wondering if anyone has done anything similar or has any kind of experience with something like this.

Paul

Paul,

Curious what type of file you are trying to get from another app.

If you are trying to get a PDF, let’s say the “test.pdf” you were using in the Android import.

Add this to you build.settings :

plist= { CFBundleURLTypes =             {                 {                     CFBundleURLSchemes =                     {                         "com.yourCompanyName.yourAppName", --example                     },                 },             }, CFBundleDocumentTypes = { { CFBundleTypeIconFiles = { "doctype-hires.png", "doctype.png" }, CFBundleTypeName = "PDF Viewer", CFBundleTypeRole = "Viewer", -- LSHandlerRank = "Owner", LSHandlerRank = "Alternate", -- LSHandlerRank = "Default", LSItemContentTypes = { "com.adobe.pdf" }, }, }, },

Even if you already have a "CFBundleDocumentTypes = "  table declared, you need to add the entire code block above for each additional file extension/type.

When you add the above code, and when you select a PDF in an email or text for example, your app will appear in the Share interface.

If you specified LSItemContentTypes = { “public.image” } instead, then when you select an image in a text, your app would appear in the Share dialog interface.

I would think this work the same when exporting from another app.  Select your registered app from the Share interface and the file URL would be passed to your apps launch args.

One place I haven’t been able to add my app as an option in the Share interface is from the iOS Photos app, it sounds like this may require an app extension, but not sure.

Maybe give this a try if it’s what you are after.

Hope this helps,

Nail

Hey Nail,

Sorry for the delayed response mate, somehow I didn’t see the email notification.

This looks like precisely what I need! Thank you!

I’ll give it a test tomorrow (I’m in the UK), but presuming it all works as expected, I’ll have to see if we can somehow make this a plugin.

To give you some context, I have a “scanner” app (not written in Corona due to its auto crop functionality that would be difficult with what we have available) that takes an image from the camera, applies OCR and then converts to PDF.

The aim now with Corona is to take these PDFs (via sharing) and upload them to an online database which the end user can view and search. Potentially clever stuff. :slight_smile:

Thanks again Nail!

Paul,

This should work for you, when I select a PDF from another app, my app shows in the Share Interface and receives the URL when selected.

I haven’t built any plugins yet, so I’m not exactly sure how they interface or modify the build.settins/info.plist.  If a plugin can modify/append the build.settings while or after it is being loaded/read, or it creates it’s own build.settings/info.plist as the app loads, then I would think a universal plugin would be possible.

Maybe someone else has an idea about this would work.

Let me know how the URLscheme goes,

Nail

Hey Nail,

Thank you so much, this works great!

I’ve only now really got one problem:

I can pick up the PDF file URL via local launchArgs = …

This works wonderful, when they app is launching for the first time. Unfortunately however, if the app is already “running” in the background, it isn’t “launched” again unlike Android, therefore there are no launchArgs.

Ideally I need to get the URL each time its sent to the app. Have you come across this at all? If so, did you manage a workaround?

Really, I do appreciate all the help you’ve given here!

Paul 

And success. We can use onSystemEvent to get the URL if the app is already running.

https://forums.coronalabs.com/topic/29720-ios-howto-opening-an-email-attachment-with-your-app/ explains this pretty well.

And with that, I think we’re done.

I’ll now put something together so we can easily do something with the files (and check we are allowed to access them) and then see if I can build a free plugin so others don’t have to go through so much pain.

Thanks again Nail, you’ve been a great help!

I’ll send you a quick PM when I get the plugin up and running (you’ll get credit of course!)

Paul

Paul,

Good Deal!   Glad you got it working.

BTW, in the Android thread, I posted both the launch args and onSystem event in the code block I posted in case you didn’t have both already implemented  :slight_smile:

I did not know Android fires the launch args everytime it opens.

Nail

We have an “Activity” plugin, but I’m not sure if it will do what you want. It brings up the activity view which is great for sharing out from your app, but that plugin isn’t designed for bringing data in. You need to build an “App Extension”

https://stackoverflow.com/questions/28248246/how-to-make-your-app-available-in-uiactivityviewcontroller-listed-activities

Corona’s simulator builds can’t do this. Intead you would probably need to work out something with Corona native builds since it involves adding an extension to your app. I’m not sure what’s involved, but you would need to go this route if it works.

Rob

Thanks Rob,

That’s more or less what I read / thought. While I’m up for the challenge (which won’t be particularly easy for someone new to Corona) I am wondering if anyone has done anything similar or has any kind of experience with something like this.

Paul

Paul,

Curious what type of file you are trying to get from another app.

If you are trying to get a PDF, let’s say the “test.pdf” you were using in the Android import.

Add this to you build.settings :

plist= { CFBundleURLTypes =             {                 {                     CFBundleURLSchemes =                     {                         "com.yourCompanyName.yourAppName", --example                     },                 },             }, CFBundleDocumentTypes = { { CFBundleTypeIconFiles = { "doctype-hires.png", "doctype.png" }, CFBundleTypeName = "PDF Viewer", CFBundleTypeRole = "Viewer", -- LSHandlerRank = "Owner", LSHandlerRank = "Alternate", -- LSHandlerRank = "Default", LSItemContentTypes = { "com.adobe.pdf" }, }, }, },

Even if you already have a "CFBundleDocumentTypes = "  table declared, you need to add the entire code block above for each additional file extension/type.

When you add the above code, and when you select a PDF in an email or text for example, your app will appear in the Share interface.

If you specified LSItemContentTypes = { “public.image” } instead, then when you select an image in a text, your app would appear in the Share dialog interface.

I would think this work the same when exporting from another app.  Select your registered app from the Share interface and the file URL would be passed to your apps launch args.

One place I haven’t been able to add my app as an option in the Share interface is from the iOS Photos app, it sounds like this may require an app extension, but not sure.

Maybe give this a try if it’s what you are after.

Hope this helps,

Nail

Hey Nail,

Sorry for the delayed response mate, somehow I didn’t see the email notification.

This looks like precisely what I need! Thank you!

I’ll give it a test tomorrow (I’m in the UK), but presuming it all works as expected, I’ll have to see if we can somehow make this a plugin.

To give you some context, I have a “scanner” app (not written in Corona due to its auto crop functionality that would be difficult with what we have available) that takes an image from the camera, applies OCR and then converts to PDF.

The aim now with Corona is to take these PDFs (via sharing) and upload them to an online database which the end user can view and search. Potentially clever stuff. :slight_smile:

Thanks again Nail!

Paul,

This should work for you, when I select a PDF from another app, my app shows in the Share Interface and receives the URL when selected.

I haven’t built any plugins yet, so I’m not exactly sure how they interface or modify the build.settins/info.plist.  If a plugin can modify/append the build.settings while or after it is being loaded/read, or it creates it’s own build.settings/info.plist as the app loads, then I would think a universal plugin would be possible.

Maybe someone else has an idea about this would work.

Let me know how the URLscheme goes,

Nail

Hey Nail,

Thank you so much, this works great!

I’ve only now really got one problem:

I can pick up the PDF file URL via local launchArgs = …

This works wonderful, when they app is launching for the first time. Unfortunately however, if the app is already “running” in the background, it isn’t “launched” again unlike Android, therefore there are no launchArgs.

Ideally I need to get the URL each time its sent to the app. Have you come across this at all? If so, did you manage a workaround?

Really, I do appreciate all the help you’ve given here!

Paul 

And success. We can use onSystemEvent to get the URL if the app is already running.

https://forums.coronalabs.com/topic/29720-ios-howto-opening-an-email-attachment-with-your-app/ explains this pretty well.

And with that, I think we’re done.

I’ll now put something together so we can easily do something with the files (and check we are allowed to access them) and then see if I can build a free plugin so others don’t have to go through so much pain.

Thanks again Nail, you’ve been a great help!

I’ll send you a quick PM when I get the plugin up and running (you’ll get credit of course!)

Paul

Paul,

Good Deal!   Glad you got it working.

BTW, in the Android thread, I posted both the launch args and onSystem event in the code block I posted in case you didn’t have both already implemented  :slight_smile:

I did not know Android fires the launch args everytime it opens.

Nail