SwiftUIでUIActivityViewControllerを表示する
SwiftUIでUIActivityViewControllerを表示する方法です。

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
struct ActivityViewController: UIViewControllerRepresentable { | |
var activityItems: [Any] | |
var applicationActivities: [UIActivity]? = nil | |
func makeUIViewController(context: UIViewControllerRepresentableContext<ActivityViewController>) -> UIActivityViewController { | |
let controller = UIActivityViewController(activityItems: activityItems, applicationActivities: applicationActivities) | |
return controller | |
} | |
func updateUIViewController(_ uiViewController: UIActivityViewController, context: UIViewControllerRepresentableContext<ActivityViewController>) {} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
struct ContentView: View { | |
@State private var showingActivityIndicator: Bool = false | |
var body: some View { | |
Button("Share swiswiswift.com") { | |
showingActivityIndicator = true | |
} | |
.sheet(isPresented: $showingActivityIndicator) { | |
ActivityViewController(activityItems: [URL(string: "https://swiswiswift.com")!]) | |
} | |
} | |
} | |
#Preview { | |
ContentView() | |
} |