Two simple iOS apps that demonstrate data sharing between applications. I put these together because I thought having both sides of the sharing flow in one place might be helpful for anyone looking to implement this functionality.
Sharing an image from OutputData to InputData, presenting a custom UI without leaving the current app
OutputData - Shows how to share images from your app using ShareLink
InputData - Shows how to receive shared images using Share Extensions
Having both apps lets you see the complete picture of how data flows between iOS applications. You can literally share from one to the other and watch it work.
SharingData/
βββ README.md
βββ .gitignore
βββ OutputData/ # Sharing app
β βββ OutputData/
β β βββ OutputDataApp.swift # App entry point
β β βββ ContentView.swift # ShareLink implementation
β β βββ Assets.xcassets/
β βββ OutputData.xcodeproj
βββ InputData/ # Receiving app
β βββ InputData/
β β βββ InputDataApp.swift # App entry point
β β βββ ContentView.swift # Main app placeholder UI
β β βββ Assets.xcassets/
β βββ ShareExtension/ # Share Extension target
β β βββ Info.plist # Extension configuration
β β βββ ShareViewController.swift # UIKit entry point
β β βββ ShareExtensionView.swift # SwiftUI interface
β βββ InputData.xcodeproj
ShareLink and Share Extensions are pretty straightforward once you see them in action, but it can be helpful to have working examples that you can actually run and test. The two apps complement each other well - you can share an image from OutputData and immediately see how InputData receives and processes it. OutputData has a simple share button, and InputData installs a Share Extension that appears in the system share sheet when sharing images specifically.
OutputData
- ShareLink API: Uses iOS 16's ShareLink component which replaces the need for the more complex
UIActivityViewControllerapproach used in earlier iOS versions:
ShareLink(item: image, preview: SharePreview("Title", image: image)) {
// Your share button UI
}- Preview System:
SharePreviewlets you customize how your content appears in the share sheet with a title and preview image.
InputData
- Share Extension Architecture: Share Extensions require a UIViewController as the entry point (
NSExtensionPrincipalClass), which is why I didn't go all-in with SwiftUI from the beginning. The UIViewController then hosts the SwiftUI view for the actual interface. - File Format Configuration: The Share Extension specifies which file types it accepts in
Info.plist. In this case, I configured it to accept images using:
<key>NSExtensionActivationRule</key>
<string>SUBQUERY(extensionItems, $extensionItem, SUBQUERY($extensionItem.attachments, $attachment, ((ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO 'public.image') AND $extensionItem.attachments.@count < 10)).@count == $extensionItem.attachments.@count).@count == 1</string>- Data Persistence: This example only demonstrates receiving and displaying shared data. If you wanted to store images for later use in the InputData app, you'd need to handle persistence using App Groups to share data between the extension and the containing app - app extensions and containing app communication is a topic in itself.
- Build and run both apps
- Open OutputData and tap the share button
- You'll see "InputData" as an option in the share sheet
- Tap it and watch how the image gets processed and displayed
- The Share Extension handles everything automatically