Sharing the content in swift have become super easy with class UIActivityViewController, In other words, we can share any contents like text, images, video and URL.
Here we will discuss how we will share our basic content using UIActivityViewController.
First, open Xcode, and create a new project. Select Single View App.
Here we will be using UITextView for entering basic text and sharing it on the click of the share button.
We will name the UITextView as “txtView” and share button as “Share” and the action for share button will be named as “shareBtnClicked”
Now we will declare some data structure to hold the values of the content which we want to share.
We have declared the variable “content” as an array of data type Any. Similarly we can declare the variables for storing the url content and image content. For instance we have declared the “urlContent” and “imageContent” for storing the URL content and image in an array data structure. However, you can use any type of data type as you like as per your need. In other words we can declare the variable “content” as String data type.
Now we are going to add action to the share button.
“pop”, “urlpop” and “imgpop” variables for displaying the action of the UIActivityViewController for sharing the text,URL and image respectively.
After that we are presenting the 3 variables, however you can use only one variable at a time.
If you use the 3 variables inside the share button action it will present only one for sharing.
On Clicking the Share button a pop over window from downside will appear to share the content to anywhere.
It will show multiple options for sharing. For instance you can copy text, save into files and many more.