facebook.init

Type Function
Return value none
Revision 2018.3381
Keywords Facebook, init
See also facebook.login()
facebook.*
fbconnect
fbinit

Overview

Sets the listener function which will receive a fbinit event once initialization of the Facebook SDK is complete.

This method also sets the same listener function as the default listener for fbconnect events, so there's no need to call facebook.setFBConnectListener() unless you want to use a different listener for fbconnect events versus the fbinit event.

Important

Initialization of the Facebook SDK is automatically triggered when the plugin is required. This call provides a way to determine when initialization is complete. We strongly recommended that you wait for initialization to complete before making calls like facebook.login() or facebook.publishInstall().

Syntax

facebook.init( listener )
listener (required)

Listener. A listener that responds to fbinit events and, optionally, fbconnect events. If this is a table, it should have a property "fbinit" 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 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 facebookListener( event )

    if ( "fbinit" == event.name ) then

        print( "Facebook initialized" )

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

    elseif ( "fbconnect" == event.name ) then

        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
end

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