4 Limitations of UIActivityViewController for iOS Sharing | by Iosif Moldovan | Apr, 2022

Share totally different content material for particular apps, deal with exercise sorts outdoors predefined iOS record, and extra

On this article we are going to have a look over the iOS content material sharing, to get a grasp of the challenges and the constraints of UIActivityViewController.

This class supplies a number of commonplace companies, equivalent to copying gadgets to the pasteboard, posting content material to social media websites, sending gadgets by way of electronic mail or SMS, and extra.

Let’s see it in motion:

That appears very simple, simply 2 strains of code.

As enter, this view controller receives a listing of things that we wish to share, by default we get assist for textual content, picture, and URLs for every other sorts you want customized assist.

As end result a modal view controller is introduced that show all of the apps within the system that’s succesful to share or carry out sure actions along with your given content material, in our case textual content, right here is the output:

Up till now, every thing seems to be simple and simple, in case you simply wish to share some content material like textual content or photos then you definately don’t want anything. However in order for you totally different content material for particular apps, issues can begin to get a bit ugly.

For instance, if you wish to share some textual content however for the mail app, you plan to share HTML because it seems to be loads higher and extra skilled.

To try this it’s a must to conform with UIActivityItemSource and implement the itemForSourceType methodology.

If shall be to take the above instance issues will appear to be this:

We received our delegate strategies carried out. However wait a minute! If we run the code within the present state we’re nonetheless not getting the HTML displayed on the mail app.

That is as a result of we didn’t set UIActivityViewController delegate, the issue right here is that this view controller doesn’t have such property accessible.

However we’ve got a fast and soiled methodology to set it up which is sort of bizarre that’s why I mark it up as a possible problem.

As it may be seen within the snippet above we cross the delegate as an merchandise within the record of things to share, this could clear up our downside. Please word, we do have a greater strategy to organising this delegate which I’ll speak about later on this article however in order for you one thing very simple and fast this strategy is usually a resolution.

By default all gadgets cross by means of exercise view controller init are concatenated then cross over to the share app. On this case, we don’t need some particular apps to share all of the gadgets we face a little bit of problem.

For instance let’s say we wish to share a textual content and a picture however particularly for the mail app we wish to share solely the HTML.

If we take the identical instance introduced above and add a picture to the record we are going to get subsequent end result:

With the above strategy, we merely cannot take away the picture from the mail app as a result of the itemForSourceType delegate methodology solely modifies the textual content merchandise, it isn’t designed to switch multiple merchandise.

Earlier within the article, I promise I’ll come again with a greater resolution in setting the UIActivityViewController delegate, and right here we’re, one resolution that ought to clear up each points.

So as an alternative of passing the self occasion within the init record we are going to cross customized objects that conform with UIActivityItemSource which can give us extra flexibility, the entire code will now develop into:

Now we’ve got complete management over what’s being shared for every app. It’s true we’ve got a bit extra advanced strategy however the management and suppleness it’s supplied needs to be price it.

Sharing HTML content material on Gmail app is usually a legitimate case, it could be true that almost all iOS customers will use the native mail app, however there are fairly a bunch that makes use of the Gmail app for his or her mailing dealing with. It’s unhappy to tell you, however in the meanwhile, is just not working correctly to share HTML over the Gmail app, and never as a result of is a few UIActivityViewController limitation, however actually is a Gmail app limitation, which solely is aware of the way to deal with textual content content material.

Here’s a screenshot on how the HTML sharing look on the Gmail app:

As it may be seen the Gmail app treats the HTML content material as plain textual content. The one factor we are able to do to enhance the person expertise is to ensure we cross plain textual content to Gmail app.

iOS system comes with a predefined record of exercise sorts for the standard social media apps, like Fb, Twitter, and so on.

The problem right here is that the iOS system can not present predefined sorts for all doable apps. So on this case we’ve got to manually determine the customized app sorts ids. For instance, let’s say we wish to share some content material over the Gmail and Pinterest apps which aren’t predefined within the iOS system.

A simple technique to discover the customized app sort id is to place a breakpoint the delegate itemForSourceType methodology and print the sort that’s given.

Right here is an instance:

More Posts