JaseTheAce
JaseTheAce

Reputation: 524

Sharing to specific Facebook page using Swift SDK GraphSharer

In my iOS app I'm trying to use the Facebook Swift SDK so that a user can post a URL to a specific Facebook page that they manage.

I've managed to get the following code working - but it only shares the post to my home timeline:

let content = LinkShareContent(url: URL(string: urlString)!)

let sharer = GraphSharer(content: content)
sharer.failsOnInvalidData = true
sharer.completion = {
    (result) in

    print(result)
}

do {
    try sharer.share()
}
catch (let error) {
    print(error.localizedDescription)
}

I believe that I need to use the graphNode property to specify the destination page and that the correct format is as follows:

sharer.graphNode = "/\(pageId)"

However, when I set this property before posting, I am getting an error:

failed(Error Domain=NSURLErrorDomain Code=-1002 "unsupported URL"

I've tried a dozen different formats to specify the node but they all return the same error and I can't find any working examples.

Upvotes: 1

Views: 378

Answers (1)

JaseTheAce
JaseTheAce

Reputation: 524

I managed to resolved this issue. It turns out that the graphNode path above is correct but I was looking in the wrong place due to the error message I was getting. Instead I just needed to specify the accessToken parameter.

graphSharer.accesstoken = myPageAccessToken 

Note: The access token is not the user's current access token, but an access token you can retrieve when calling the /me/accounts API.

var graphPath = "/me/accounts"

I also then had an issue posting to the page because I had only requested the publish_actions permission. From the documentation, this appears to be suitable but it only worked where I requested further permissions as follows:

loginManager.logIn(withPublishPermissions: ["publish_actions", 
    "manage_pages", "publish_pages"], ...

Hopefully this will be of use to someone in future. :-)

Upvotes: 1

Related Questions