How to integrate twitter on any device

What I’m going to do is use the social api if it’s available.  If not, then use functionality similar to the “tweet button”.  Open up a new browser on the device prepopulated with a tweet field.  If the user is not logged into twitter on their browser then the page also has login fields on it.

function showTwitterPopup(event)
    if event ~= nil and event.phase ~= “ended” then return true end
  
    local isTwitterAvailable = native.canShowPopup( “social”, “twitter” )    
    if isTwitterAvailable then
    
        local message = “I just scored " …
            tostring(playerWicketsLost) … “/” … tostring(playerRunsScored) …
            " and " … getMatchSummaryWonByText(true) … " against " … computerTeamAbbrev …
            " in The Best Cricket Game Ever.  Can you beat my score?”
            
        local options = {
           service = “twitter”,
           message = message,
           url = “http://bestcricketgame.com”,
           listener = twitterListener
        }
        
        native.showPopup(“social”, options)
    else
        local message = “I just scored " …
            tostring(playerWicketsLost) … “/” … tostring(playerRunsScored) …
            " and " … getMatchSummaryWonByText(true) … " against " … computerTeamAbbrev …
            " in The Best Cricket Game Ever.  Can you beat my score? http://www.bestcricketgame.com
        event.target.url = “https://twitter.com/intent/tweet?text=” … url_encode(message)
        openWebsite(event)
    end
end
 

function url_encode(str)
  if (str) then
    str = string.gsub (str, “\n”, “\r\n”)
    str = string.gsub (str, “([^%w %-%_%.%~])”,
        function © return string.format ("%%%02X", string.byte©) end)
    str = string.gsub (str, " ", “+”)
  end
  return str    
end

function openWebsite(event)
    if event and event.phase ~= “ended” then return true end
    
    if event.target.url == nil then
        print(“no target url”)
    else
        system.openURL(event.target.url)
    end
    
    return true
end
 

Hope you find it useful :slight_smile:

I found this very helpful. Thanks

FYI the forum formatted “( c )” into ©, so one of the above lines should read “function ( c ) return string.format (”%%%02X", string.byte( c )) end)"

I found this very helpful. Thanks

FYI the forum formatted “( c )” into ©, so one of the above lines should read “function ( c ) return string.format (”%%%02X", string.byte( c )) end)"