Share Sheet Extension Development for iOS
Share Sheet Extension (type NSExtensionPointIdentifier: com.apple.share-services) appears in the system share menu when the user taps "Share" in Safari, Photos, Files, or any other application. This allows you to accept content directly—URL, image, file—without switching between applications.
NSItemProvider — The Heart of Share Extension
Incoming content arrives through extensionContext?.inputItems as an array of NSExtensionItem. Each contains attachments: [NSItemProvider]. You must check hasItemConformingToTypeIdentifier for the needed UTI and load data through loadItem(forTypeIdentifier:options:completionHandler:).
This is asynchronous. And this is where problems start: developers often load data in viewDidLoad() without waiting for completion and try to use the result synchronously. The extension crashes or shows empty data.
Correct approach for loading a URL from Safari:
guard let item = extensionContext?.inputItems.first as? NSExtensionItem,
let provider = item.attachments?.first,
provider.hasItemConformingToTypeIdentifier(UTType.url.identifier) else { return }
provider.loadItem(forTypeIdentifier: UTType.url.identifier) { [weak self] url, error in
DispatchQueue.main.async {
self?.receivedURL = url as? URL
self?.updateUI()
}
}
Supporting multiple UTIs. The user may select multiple files. NSExtensionItem may contain multiple attachments. Iterate through all, check each via hasItemConformingToTypeIdentifier. Don't forget about kUTTypeFileURL vs kUTTypeURL—these are different types.
Info.plist: NSExtensionActivationRule
This rule determines when your extension appears in the share menu. Poor configuration means the extension appears everywhere unwanted and annoys users.
Basic rule via NSExtensionActivationRule dictionary:
<key>NSExtensionActivationSupportsWebURLWithMaxCount</key><integer>1</integer>
<key>NSExtensionActivationSupportsImageWithMaxCount</key><integer>5</integer>
For complex logic, use an NSPredicate string in NSExtensionActivationRule. For example, to show only for PDF files: SUBQUERY(extensionItems, $item, SUBQUERY($item.attachments, $att, $att.registeredTypeIdentifiers UTI-CONFORMS-TO "com.adobe.pdf").@count >= 1).@count >= 1.
App Group and Data Transfer to the Main Application
Share Extension lives in a separate process. Data from the extension doesn't reach the main application automatically.
Scheme: the extension saves data to App Group container (UserDefaults(suiteName:) or file in containerURL), the main application reads from the same place on next launch. For immediate processing—open the main application via extensionContext?.open(URL(string: "yourapp://share-received")!). But this closes Share Sheet and switches the user—not always desirable.
From iOS 16, you can use SwiftUI in Share Extension through ShareViewController based on UIHostingController. However, UIHostingController in Extension behaves differently: doesn't support @Environment(\.dismiss) for closing via extensionContext?.completeRequest(returningItems:). Close only via extensionContext.
Typical Work Structure
Audit incoming data types to accept. Configure NSExtensionActivationRule. Develop extension UI (compact—max one-third of screen). Integrate via App Group. Handle cancellation (didSelectCancel) and completion (didSelectPost).
Timeline
Simple Share Extension (accept URL or image, show form): 2–4 weeks. Extension with complex logic, multiple file types, server synchronization: 4–7 weeks. Cost is calculated after analyzing content types and UI requirements.







