Add AirDrop sharing to your iOS app using UIActivityViewController with file URLs. UIActivityViewController is a UIKit class that provides system sharing services including AirDrop, Mail, Messages, and more. When you present it with file URLs, AirDrop automatically appears as an option on compatible devices. iOS handles device discovery, connection, and data transfer through Bluetooth and peer-to-peer Wi-Fi. You only need a few lines of Swift code.
What Is UIActivityViewController and How Does It Enable AirDrop?
UIActivityViewController is a UIKit view controller that presents system sharing services to users. Apple introduced it in iOS 6 and expanded it with AirDrop support in iOS 7. When you initialize it with activity items like file URLs, images, or text, it displays all compatible services. AirDrop appears when the device has Bluetooth and Wi-Fi enabled and another compatible device is nearby.
The class handles the full sharing workflow. It scans for nearby devices, establishes a peer-to-peer connection, and transfers the data. You do not manage any networking code yourself.
How to Share Files with AirDrop Using UIActivityViewController
Pass file URLs to the UIActivityViewController initializer. iOS reads the file type from the URL extension and determines which services can handle it.
let fileURL = Bundle.main.url(forResource: "document", withExtension: "pdf")!
let activityController = UIActivityViewController(
activityItems: [fileURL],
applicationActivities: nil
)
present(activityController, animated: true, completion: nil)
The activityItems parameter accepts an array of Any. Pass URLs for file sharing, UIImage objects for photo sharing, or String objects for text sharing. The applicationActivities parameter accepts custom UIActivity subclasses. Pass nil to use only system-provided services.
How to Customize Available Sharing Activities
Set the excludedActivityTypes property to remove unwanted services from the sharing sheet. This streamlines the user experience by showing only relevant options.
activityController.excludedActivityTypes = [
.postToWeibo,
.message,
.mail,
.print,
.copyToPasteboard
]
Each UIActivity.ActivityType constant represents a system service. Common types include .airDrop, .message, .mail, .postToFacebook, .postToTwitter, .copyToPasteboard, .print, .saveToCameraRoll, .assignToContact, and .addToReadingList. Exclude types that do not make sense for your content.
How to Handle AirDrop on iPad with Popover Presentation
UIActivityViewController presents as a popover on iPad. You must configure the popoverPresentationController with a source view or bar button item. Without this configuration, the app crashes on iPad.
if let popover = activityController.popoverPresentationController {
popover.barButtonItem = navigationItem.rightBarButtonItem
}
Use barButtonItem when triggering from a navigation bar or toolbar button. Use sourceView and sourceRect when triggering from a custom view. Set the permittedArrowDirection to control popover placement.
What Are Uniform Type Identifiers and How Do They Affect AirDrop?
Uniform Type Identifiers (UTIs) are string identifiers that describe data types. Apple defines standard UTIs for common formats. com.adobe.pdf represents PDF documents. public.png represents PNG images. public.jpeg represents JPEG images. com.microsoft.word.doc represents Word documents.
iOS uses UTIs to determine which app opens a received file. When a user receives a file via AirDrop, iOS checks the UTI against registered apps. Multiple apps can register for the same UTI. In that case, iOS prompts the user to choose.
Register your app for custom UTIs in Info.plist under Exported Type Identifiers. Use the importedDocumentTypes or Exported Type Identifiers key depending on whether your app creates or consumes the type.
How to Receive Files via AirDrop in Your App
To receive files sent via AirDrop, your app must declare support for the appropriate UTIs. Open Info.plist and add the CFBundleDocumentTypes key. Each entry specifies a UTI and the role your app plays.
<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>CFBundleTypeName</key>
<string>PDF Document</string>
<key>LSHandlerRank</key>
<string>Alternate</string>
<key>LSItemContentTypes</key>
<array>
<string>com.adobe.pdf</string>
</array>
</dict>
</array>
Implement application(_:open:options:) in your AppDelegate to handle incoming files. iOS calls this method when the user opens a received AirDrop file in your app.
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool {
// Copy the file to your app's documents directory
let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let destinationURL = documentsDirectory.appendingPathComponent(url.lastPathComponent)
try? FileManager.default.copyItem(at: url, to: destinationURL)
return true
}
Set the LSHandlerRank to Owner if your app is the primary handler for the UTI. Set it to Alternate if other apps also handle the same format.
How to Handle Completion and Error Callbacks
UIActivityViewController provides a completionWithItemsHandler closure. Use it to respond to the sharing result.
activityController.completionWithItemsHandler = { activityType, completed, returnedItems, error in
if completed {
print("Shared via (activityType?.rawValue ?? "unknown")")
} else if let error = error {
print("Sharing failed: (error.localizedDescription)")
}
}
The completed boolean indicates whether the user finished the sharing operation. The error parameter contains system-level errors. The returnedItems array contains data from services that return information to the app.
Practical Tips for AirDrop Integration in iOS
Test AirDrop on physical devices. The iOS Simulator does not support AirDrop. You need at least two iOS devices or a Mac for testing.
Use file URLs for document sharing. UIActivityViewController accepts file URLs and shares the underlying file. The receiving device opens it in the appropriate registered app.
Exclude activities thoughtfully. Removing too many options frustrates users who expect certain sharing channels. Remove only activities that are incompatible with your content.
Always configure the popover presentation controller on iPad. Failing to do so causes a runtime crash.
Call present on the correct view controller. Ensure the presenting view controller is visible and not already presenting another controller.
Use UTIs to declare custom file types your app supports. Set the LSItemContentTypes array in Info.plist to register supported UTIs. This ensures your app appears as an AirDrop destination for your custom file format.
Summary
This tutorial covered the complete implementation process from setup to working code. You now have a functional implementation that you can extend and customize for your own projects. The same patterns apply to related features in the iOS ecosystem.
Frequently Asked Questions
Can I test this on the iOS Simulator? Some features require a physical device with a compatible processor. The article notes specific sections where the Simulator is sufficient and where a real device is required.
What iOS version is required? UIActivityViewController has been available since iOS 6, and AirDrop appears in the share sheet from iOS 7 on.
What are the prerequisites for this tutorial? You need Xcode installed on a Mac and basic familiarity with Swift programming. No prior experience with the specific framework is required.
Can I use this with SwiftUI instead of UIKit? The article demonstrates the UIKit approach. SwiftUI has its own APIs for similar functionality. You can adapt the concepts, but the implementation details differ between the two frameworks.