Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Use SwiftUI in Action Extension
import UIKit
import MobileCoreServices
import SwiftUI
class ActionViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Get the item[s] we're handling from the extension context.
// For example, look for an image and place it into an image view.
// Replace this with something appropriate for the type[s] your extension supports.
for item in self.extensionContext!.inputItems as! [NSExtensionItem] {
for provider in item.attachments! {
if provider.hasItemConformingToTypeIdentifier("public.url") {
provider.loadItem(forTypeIdentifier: "public.url", options: nil, completionHandler: { (url, error) -> Void in
if let shareURL = url as? NSURL {
let parsedUrl = URL(string: shareURL.absoluteString!)!
print("find url = ", parsedUrl)
// build the SwiftUI view, wrap it in a UIHostingController then send to the main thread to update the UI
let contentView = SecondView(url: parsedUrl.absoluteString, clickedDone: self.done)
let viewController = UIHostingController(rootView: contentView)
DispatchQueue.main.async {
self.present(viewController, animated: true)
}
}
})
}
}
}
}
func done() {
// Return any edited content to the host app.
// This template doesn't do anything, so we just echo the passed in items.
self.extensionContext!.completeRequest(returningItems: self.extensionContext!.inputItems, completionHandler: nil)
}
}
import SwiftUI
struct SecondView: View {
@State var url: String
var clickedDone: () -> Void
var body: some View {
VStack {
Button("Done") {
self.clickedDone()
}
Text(url).font(.system(size: 36))
Text("Loaded by SecondView").font(.system(size: 14))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment