1
class ShareSheetViewModel: UIViewController, ObservableObject, SharingDelegate {
    func sharer(_ sharer: Sharing, didCompleteWithResults results: [String : Any]) {
        if sharer.shareContent?.pageID != nil {
            print("Share: Success")
        }
    }
    func sharer(_ sharer: Sharing, didFailWithError error: Error) {
        print("Share: Fail")
    }
    func sharerDidCancel(_ sharer: Sharing) {
        print("Share: Cancel")
    }
func actionFB(){
        let shareContent = ShareLinkContent()
        shareContent.contentURL = URL.init(string: "https://developers.facebook.com")! //your link
        shareContent.quote = "Text to be shared"
        let dialog = ShareDialog(viewController: self, content: shareContent, delegate: self
        )
        dialog.show()
  }
}
ShareSheetViewModel.swift
            Button(action: {
                         filterVM.actionFB()
                            }, label: {
                                Group{
                                    HStack {
                                        Image("FbIcon")
                                            .resizable()
                                            .frame(width: 20, height: 20)
                                        
                                        Text("Facebook")
                                            .font(.custom("PublicSans-Regular", size: 14))
                                            .foregroundColor(.white)
                                            .padding(.leading, 8)
                                        
                                        Spacer()
                                    }
                                    .padding(.horizontal, 16)
                                }
                            })

ShareComponent.swift

I can't share link my url to the Facebook app directly.

Is there a solution for my issue?

reference : Share on Facebook IOS 11 Swift

  • Since `ShareSheetViewModel` is actually presenting it has to be wrapped in a `UIViewControllerRepresentable` and put in the `body`. With your current setup you are mixing the `View` that is what the user sees with the logic portion. – lorem ipsum Nov 05 '21 at 12:09
  • 1
    i get an error when adding `ShareDialog(viewController: self, content: shareContent, delegate: self)` in the body `class` so there must be a `UIViewController` to avoid error , and when the error occurs it is in `viewController:self` i got error just like this `Cannot convert value of type 'ShareSheetViewModel' to expected argument type 'UIViewController?'` so if added `UIViewControllerRepresentable` and put in the my`body` – Kiki Triansyah Nov 08 '21 at 05:30

0 Answers0