简体   繁体   中英

Facebook SDK for iOS: FBSDKShareDialog is not shown

I am a newbie in iOS, and I would like to share a link using the Facebook SDK for iOS. My code's as follows:

@IBAction func shareVoucherUsingFacebook(sender: UIButton) {
    print("Facebook")
    let content : FBSDKShareLinkContent = FBSDKShareLinkContent()
    content.contentURL = NSURL(string: "www.google.com")
    content.contentTitle = "Content Title"
    content.contentDescription = "This is the description"

    let shareDialog: FBSDKShareDialog = FBSDKShareDialog()

    shareDialog.shareContent = content
    shareDialog.delegate = self
    shareDialog.fromViewController = self
    shareDialog.show()

}

I have also implemented the FBSDKSharingDelegate methods, but I am not able to receive the share dialog when I press this button, and the method func sharer(sharer: FBSDKSharing!, didFailWithError error: NSError!) is being executed, which means something is going wrong, but I can't figure it out.

Thanks in advance.

changing this line

content.contentURL = NSURL(string: "www.google.com")

into

content.contentURL = NSURL(string: "https:\\www.google.com")

worked with me

this is the delegate methods I used

func sharer(sharer: FBSDKSharing!, didCompleteWithResults results: [NSObject: AnyObject])
{
    print(results)
}

func sharer(sharer: FBSDKSharing!, didFailWithError error: NSError!)
{
    print("sharer NSError")
    print(error.description)
}

func sharerDidCancel(sharer: FBSDKSharing!)
{
    print("sharerDidCancel")
}

Just remember to add http:// to your contentURL if you are using NSURL(string:"") in your code.

let content : FBSDKShareLinkContent = FBSDKShareLinkContent()
content.contentURL = NSURL(string: "http://www.google.com")
content.contentTitle = "Content Title"
content.contentDescription = "This is the description"

FBSDKShareDialog.showFromViewController(self, withContent: content, delegate: self)

实现委托方法对我有用

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM