Many apps share content with other applications. The object used to perform data sharing between apps is called an activity view controller. In this video, see how to use activity view controllers.
- [Narrator] Another common UI task in IOS is to share data from your app to another app. For example, you might want to take the text that's inside of your app and allow your user to send it as a text message to another person. Let's take a look at how that process works. In our code, go into the did press button method and create a constant called activity view, and set it equal to UI activity view controller, and instantiate that.
Notice that there's two parameters, activity items and application activities. Activity items is an array of items that you're going to send to the other application. Here all we're going to do is just send a string. Just type anything that you want in the string there, just make sure you remember what it is so that you can recognize it later on. For application activities, you can specify custom activities that your application supports, which for what we're doing, is not necessary.
So just put in no right there and then all we need to do is present this view controller. So call the present method and then pass in the activity view that you created, specify whether you want it to be animated, I'll put in true, and then if you want a completion handler for when the activity view controller is dismissed, you can handle it right here. For now we're not interested in that, so just put no. That's it, that's all you have to do. Now run the application and in the running app, hit the button to show the activity view controller.
So touch the button right here, and then I have a list of options. These are applications that support the type of data I have selected. Now I've tested this in the simulator and messages and reminders just crashed the application. This is something you'll have to actually test on a physical device to see it working properly. But on the simulator, you can test the copy button. Notice that you also see, this is new with IOS 13, the data that is being sent to the other app.
So you can see this little icon that shows a T for text and then a caret for the cursor, and then you have that text that was in our application. So let's just hit the copy button, and we can see if that copied to the clipboard. Head back over to x code, you can stop the running application, hide the debug area, and here I'm going to delete the text inside of my string, and then press Command V on my keyboard to paste anything on my clipboard and sure enough I get that text.
If you happen to not get that text, you can always go back to the simulator, and then what you want to do is go to Edit, and then choose Automatically Sync Pasteboard, and try that again. So, if you want to share data with another application, of course first, it has to be an application that supports that type of data, you need to create an activity view controller, passing in an array of data items that you want to send to another application, and then you call the present method to show the activity view controller, so the user can share your data with another application
- Advanced UI controls
- Creating alert controllers
- Displaying web content
- Creating a collection view controller
- Creating a custom data type
- Creating tabbed apps
- Importing and setting icons for your apps
- Publishing your app to the App Store