Add Share extension and Action extension respectively in Xcode. We can use the same code to both extension
SwiftUI
I usually make a ShareView in SwiftUI with ShareViewModel to control the logic
struct ShareView: View {
@ObservedObject var vm: ShareViewModel
var body: some View {
NavigationStack(path: $vm.routes) {
List {}
}
}
}
In ShareViewController, we can just conform to UIViewController and add our SwiftUI view as child view controller
let vm = ShareViewModel()
let hostingController = UIHostingController(rootView: ShareView(vm: vm)
addChild(hostingController)
view.addSubview(hostingController.view)
hostingController.didMove(toParent: self)
hostingController.view.constrainEdges(to: view)
We can use this same ShareViewController for both Share and Action extension, so add it to both targets.
Info.plist
In Share extension Info.plist, we can replace the default NSExtensionActivationRule of TRUEPREDICATE with the below.
I also use code instead of storyboard, so I use NSExtensionPrincipalClass instead of MainInterface
Add Share extension and Action extension respectively in Xcode. We can use the same code to both extension
SwiftUI
I usually make a
ShareView
in SwiftUI withShareViewModel
to control the logicIn ShareViewController, we can just conform to
UIViewController
and add our SwiftUI view as child view controllerWe can use this same
ShareViewController
for both Share and Action extension, so add it to both targets.Info.plist
In Share extension Info.plist, we can replace the default
NSExtensionActivationRule
ofTRUEPREDICATE
with the below. I also use code instead of storyboard, so I useNSExtensionPrincipalClass
instead ofMainInterface
In Action extension Info.plist, it looks like below
Icon
For Action extension, we need an icon to show in system Share dialog. We can add new App Icon to
Media
asset catalogNote that we need to specify this
AppIcon
inPrimary App Icon Set Name