Social

Lots of apps allow their users to share something in social networks. Photos apps allow us to share cat pictures in Facebook, news apps allow us to share some neat inventions of British scientists, etc.

It may look complicated on a first sight to allow users to share some info. Render a view where the user will be able to add the text, add image view for a message's image, parse URL in the message if any, then ask the user where he wants to post it, authenticate him, post a message, etc. So many tasks, OMG!

But in fact, it is pretty easy. iOS already has instruments that help developers to post whatever users want to social networks, into emails, messages, etc.

By default iOS allows users to share messages to Facebook and Twitter if they are logged in their accounts, by email, and with Messages app. Also, if a message has an image, it can be saved into Photos or assigned to some contact. If the message has a URL, it can be opened in Safari. In addition, the message can be copied or printed.

Of course this behavior can be changed - you can remove some actions or add your own.

In this tutorial, we are going to share an image that user downloaded or picked from his library in tutorial_app.

As usual, let's start with layout. We are going to add one simple Share button below the image:

        class FilesScreenLayout < MotionKit::Layout
          def layout
            add UITextField, :url
            add UIButton, :get
            add UIButton, :photo
            add UIButton, :library
            add UIImageView, :image
            add UIButton, :share
          end

          # rest of the layout code ...

          def share_style
            title "Share"
            title_color :blue.uicolor
            constraints do
              center_x.equals(:superview, :center_x)
              top.equals(:image, :bottom).plus(20)
            end
          end
        end
      

And now let's make this small button work. First we need to assign an action to a button. Let's call a method that will show sharing options share. Inside of this method, we need to gather everything that we want to share. It can be text, images and URLs. In this example, if a picture is not present, we will show an alert view. If the image is present, we are composing sharing sheet, and showing it to the user as a typical screen. Sharing Sheet here is a small view that will be presented to a user, where he will be able to choose where he wants to post given data:

        class FilesScreen < PM::Screen
          title "Files and Images"

          # screen code ...

          def on_load

            # on_load code ...

            @layout.get(:share).on_tap { share }

            # rest of on_load code ...
          end

          # rest of the screen code ...

          def share
            # create objects that we want to share. Can be text, images and URLs:
            message = "Check this out!"
            image = @image_view.image

            # we don't want to share just a plain text:
            if !image.is_a?(UIImage)
              UIAlertController.alert self, "Error",
                message: "Please add an image first.",
                buttons: ["OK"]
              return
            end

            shareable_items = [message, image]
            sharing_screen = UIActivityViewController.alloc.initWithActivityItems shareable_items,
              applicationActivities: nil
            open_modal sharing_screen
          end
        end
      

This small change in the code will allow the user to post messages into default social networks, or share it with default apps. For example, if you will log in to your Twitter account in simulator's settings app you will have something like this:

And when sharing with Twitter:

You can see that items array has been added to the message automatically. iOS recognized an image and a text. A third possible data type that you could pass is an NSURL, which would represent some URL.

Since we just finished customizing our screens, let's commit all changes:

        git add .
        git commit -m "Sharing Sheet added"
      

Summary

It is super easy to allow users to share anything in your app. Just decide what info needs to be shared, create an instance of UIActivityViewController with shareable items, and show it as a screen. The iOS will manage all other things. For a further customization, please check Apple Docs. If you need something more powerful with more available actions, you could try to use ShareKit.

Book Index | Next