Dismiss share extension from custom SwiftUI view

701 Views Asked by At

I am using custom SwiftUI view from main target for sharing document from ShareViewController of share extension. Using Navigation link and sharing the document after navigating through three views. document is uploaded without any problem, but I don't know how to close the views after upload is done.

This is how navigation looks like

ShareViewController(SLComposeServiceViewController) -> PropertyListView -> UnitListView -> UploadView

and didPost looks like this

override func didSelectPost() {
    print("In Did Post")
    if let item = self.extensionContext?.inputItems[0] as? NSExtensionItem{
        print("Item \(item)")
        print(item.attachments)
        print(item.attachments![0])
        let itemProvider = item.attachments![0]
        if itemProvider.hasItemConformingToTypeIdentifier("com.adobe.pdf"){
            itemProvider.loadItem(forTypeIdentifier: "com.adobe.pdf", options: nil) { (item, error) in
                if error != nil{
                    print(error!.localizedDescription)
                }else{
                    if let url = item as? URL{
                        print(url)
                        DispatchQueue.main.async{
                            //saving to user defaults
                            let dict: [String : Any] = ["dcument" :  url.absoluteString, "name" : self.contentText.isEmpty ? url.lastPathComponent : self.contentText!]
                            let savedata =  UserDefaults.init(suiteName:"group.in.pixbit.hijricalendar")
                            savedata?.set(dict, forKey: "sharedDocument")
                            savedata?.synchronize()
                        //loading swiftui view
                            let swiftuiView = NavigationView{PropertyListView()}
                            let vc = UIHostingController(rootView: swiftuiView)
                            let newView = vc
                            self.view.window?.rootViewController = newView
                            self.view.window?.makeKeyAndVisible()

                        }
                    }
                }
            }
        }
    }
}
1

There are 1 best solutions below

1
Peter Friese On

You need to call completeRequest(returningItems:completionHandler:) on the extensionContext of your view controller.

Here is a code snippet from one of my apps:

  override func didSelectPost() {
    let artifact = Artifact(title: self.contentText,
                            author: self.metaAuthor,
                            url: url?.absoluteString ?? metaUrl ?? "",
                            imageUrl: metaImage,
                            siteName: self.siteName,
                            dateAdded: Date(),
                            excerpt: metaDescription,
                            notes: "",
                            tags: nil)
    self.artifactRepository?.addArtifact(artifact)
    
    self.extensionContext?.completeRequest(returningItems: [], completionHandler:nil)
  }