facebook.setFBConnectListener

Type Function
Return value none
Revision Release 2024.3703
Keywords Facebook, setFBConnectListener
See also facebook.login()
facebook.*
fbconnect

Overview

Sets the listener function which will receive fbconnect events. This method can be used when the listener needs to change but there’s no need for a facebook.login(), facebook.request(), or facebook.showDialog() call.

Note

The default listener is the one supplied to facebook.init(), so using this API is not mandatory.

Syntax

facebook.setFBConnectListener( listener )
listener (required)

Listener. A listener that responds to fbconnect events. If this is a table, it should have a property "fbconnect" that is a function.

Example

local facebook = require( "plugin.facebook.v4a" )

-- Check for a value inside the provided table
local function valueInTable( t, valueToFind )
    for k,v in pairs( t ) do
        if v == valueToFind then
            return true
        end
    end
    return false
end

local function facebookConnectListener( event )
    if ( "session" == event.type ) then
        -- Handle login event and try to share the link again if needed
    elseif ( "dialog" == event.type ) then
        -- Handle dialog event
    end
end

local function shareLink( url )

    local accessToken = facebook.getCurrentAccessToken()
    if accessToken == nil then
        facebook.login()
    elseif not valueInTable( accessToken.grantedPermissions, "publish_actions" ) then
        facebook.login( { "publish_actions" } )
    else
        facebook.showDialog( "link", { link=url } )
    end
end

local function facebookInitListener( event )

    print( "Facebook initialized" )

    -- Initialization complete; share a link
    shareLink( "https://www.coronalabs.com/" )
end

-- Set the "fbconnect" listener 
facebook.setFBConnectListener( facebookConnectListener )

-- Set the "fbinit" listener to be triggered when initialization is complete
facebook.init( facebookInitListener )