Introduction to UIDocumentInteractionController Swift:
In this post we will learn how to create UIDocumentInteractionController in Swift language. We will create an example app to learn how to use UIDocumentInteractionController in swift. According to Apple docs, UIDocumentInteractionController is a viewcontroller that previews, open, or print files whose file format cannot be handled directly by your app. In short, UIDocumentInteractioncontroller allows us (developers) to open different files using other apps installed on iPhone or iPad device.
You can check objective C version for UIDocumentInteractionController from below link
Implementation for UIDocumentInteractionController in swift:
Create a new project and select single view application from app templates and name it “UiDocumentController-Swift”. Change your Language from objective C to swift.
Press Next and then select location for your project where you want to save the project, press create to create your project.
Open ViewController.swift file. Now we will create variable for UIDocumentInteractionController.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Open your Main.storyBoard and drag UIButton to it. It’s time to create an IBAction for our button so that we can open UIDocumentInteractionController on pressing it.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Connect your button dragged in last step on Main.storyBoard with your IBAction to UIButtonTouchUpInside event.
Open ViewController.swift file and now we will write code to present UIDocumentInteractionController to the user.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
In above code, first we get file url which we want to share using UIDocumentInteractionController (in our case its an image place locally in our project). After we get file path we created our UIDocumentInteractionController by using init method and pass the file url to it. Lastly, we user presentOptionsMenuFromRect method to show UIDocumentInteractionController to the user.
PresentOptionMenuFromRect take three parameters
1) frame: from where we want to the menu
2) inView: in which we want to show the UIDocumentInteractionController
3) animated: wether you want to present your UIDocumentInteractionController animated or not
Complete Code for UIDocumentInteractionController:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
In this post your leaned. how to create UIDocumentInteractionController in swift language. We created example app that creates UIDocumentInteractionController using swift language and showcased how can we create and use UIDocumentInteractionController using swift language. If you are looking for Objective C Version of the UIDocumentInteractionController you can check it from here
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies.
This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the ...
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.