Search code examples
iosswiftfacebookshareios11

Share on Facebook IOS 11 Swift


In versions below 11 ShareButton to facebook works successfull, but in 11 version this function doesn't work. What can u advice?

if SLComposeViewController.isAvailable(forServiceType: SLServiceTypeFacebook) {
        var vc: SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
        vc.setInitialText("My text")
        self.present(vc, animated: true, completion: nil)
    }

Solution

  • You can use the facebook sdk to share content Like

    Step 1:

    install pod :- pod 'FacebookShare'

    Step 2:

    import FBSDKShareKit

    Step 3:

    func shareTextOnFaceBook() {
        let shareContent = ShareLinkContent()
        shareContent.contentURL = URL.init(string: "https://developers.facebook.com")! //your link
        shareContent.quote = "Text to be shared"
        ShareDialog(fromViewController: self, content: shareContent, delegate: self).show()
    }
    
    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")
    }
    

    Step 4: Must add this in Info.plist

    <key>LSApplicationQueriesSchemes</key>
    <array>
            <string>fbauth2</string>
    </array>