Reputation: 13
Apps like ILovePDF, Google Chrome, and others have this feature in the 3rd row of the iOS share sheet where you can add a custom action like "Open in Chrome" and "Edit with Ilovepdf". Can anyone help me out on how to achieve this functionality?
Custom actions in iOS ShareSheet - screenshot
All the resources I've got on the internet show only the implementation of custom actions in my app's share sheet, but not in another app's.
All the articles I referred to regarding integrating custom actions, talk about integrating them within the share sheet of the app you're building and not how to show those actions in other apps' share sheet as well
Upvotes: 0
Views: 703
Reputation: 925
I guess what you are looking for is an Action extension.
See this Apple guide for details: https://developer.apple.com/library/archive/documentation/General/Conceptual/ExtensibilityPG/Action.html#//apple_ref/doc/uid/TP40014214-CH13-SW1
Upvotes: 0
Reputation: 2093
Yes, you add the Share Extension
to your targets and build your custom Share View.
1. Add the Share Extension to your app's project by going to your app's project page and clicking on the plus icon or, File > New > Target and look for Share Extension
2. Build a Custom View even using SwiftUI in your newly created extension. I'm going to provide a template View here as an example:
import UIKit
import Social
import SwiftUI
class ShareViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
//view.backgroundColor = .red
/// Interactive Dismiss Disabled
isModalInPresentation = true
if let extensionContext = extensionContext,
let itemProvider = (extensionContext.inputItems.first as? NSExtensionItem)?.attachments {
let hostingView = UIHostingController(rootView: ShareViewTemplate(itemProvider: itemProvider,
extensionContext: extensionContext))
hostingView.view.frame = view.frame
view.addSubview(hostingView.view)
}
}
}
fileprivate struct ShareViewTemplate: View {
//MARK: - Properties
var itemProvider: [NSItemProvider]
var extensionContext: NSExtensionContext?
var body: some View {
VStack {
Text("Your Custom View using SwiftUI")
}
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .center)
.overlay(alignment: .topLeading) {
Button("", systemImage: "xmark") {
dismiss()
}
.padding()
}
}
private func dismiss() {
extensionContext?.completeRequest(returningItems: [])
}
}
3. When you run the app you will be asked on what application you want the share extension to be run. Say I chose the Gallery:
4. Then in your chosen app, for example, the Photos
app, when using the share option you will see your custom app too:
5. When clicking on it you will be presented with your custom SwiftUI
View:
I hope to have answered your question! Let me know!
Upvotes: 1