How to make sharing menu in SwiftUI for macOS
Published in
Dec 23, 2020
Use NSSharingService.sharingServices(forItems:)
with an array of one empty string gives a list of sharing items. There we show image
and title
of each menu item
import SwiftUI
import AppKit
import EasySwiftUIextension NSSharingService {
static func submenu(text: String) -> some View {
return Menu(
content: {
ForEach(NSSharingService.sharingServices(forItems: [""]), id: \.title) { item in
Button(action: { item.perform(withItems: [string]) }) {
Image(nsImage: item.image)
Text(item.title)
}
}
},
label: {
Text("Share")
Image(systemName: SFSymbol.squareAndArrowUp.rawValue)
}
)
}
}
Alternative, you can trigger NSSharingServicePicker
from a button, it shows a context menu with sharing options