Dec 212010
 

THIS TUTORIAL IS NOW DEPRECIATED – PLEASE USE THE UPDATED FACEBOOK TUTORIAL.

This tutorial will show you how to add Facebook posting to your iPhone app written using Corona, the best mobile development engine there is.

*Edited to fix a line in the code that didn’t belong, many thanks to jmp909 for pointing it out; this is what happens when you attempt to write a tutorial in the middle of the night!

Nothing new and fancy here; it’s the same old Facebook posting with a new API – that said, it does feel slightly less clunky and lacks the gray flash one often saw when posting using the old version.

It’s 2 AM here so this will be as short and sweet as possible as I was up at 10 AM and after a long dentist visit my mouth is killing me. (Not my teeth, they’re fine – it was just for two fillings. I had to have many needles however and they’ve left half of my mouth in agony.)

Alright, let’s get this done!

STEP ONE: Open your main.lua file or, if you are using director, your gameover.lua file, assuming that is where you will have your Facebook button. Then, insert this near the top of the file where you require any appropriate files;

1
local facebook = require ("facebook")

STEP TWO: Make sure you have a button on the screen, then paste this code somewhere beneath it;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
local function printTable( t, label, level )
if label then print( label ) end
level = level or 1
if t then
for k,v in pairs( t ) do
local prefix = ""
for i=1,level do
prefix = prefix .. "\t"
end

print( prefix .. "[" .. tostring(k) .. "] = " .. tostring(v) )
if type( v ) == "table" then
print( prefix .. "{" )
printTable( v, nil, level + 1 )
print( prefix .. "}" )
end
end
end
end

-- The above code should not be modified in anyway

local function pressfb (event)
if event.phase == "ended" then
local function postit( event )
print( event.name )
print( event.type )

if ( "session" == event.type ) then

-- The above code is to use a button to publish, if you would prefer it automatically popped up remove the first two lines of the above and two "end"s at the bottom of this code.


local attachment = {
name = "Download Happy Doodle Sushi Smile To Compete With Me",
link = "http://itunes.apple.com/us/app/happy-doodle-sushi-smile/id404532572?mt=8",
caption = "Think you can beat my score of " .. tostring(myscore) .. "? I dare you to try!",
picture = "http://peachpellen.com/hdss.png",
actions = json.encode( { { name = "Get It Now!", link = "http://itunes.apple.com/us/app/happy-doodle-sushi-smile/id404532572?mt=8" } } )
}

-- Change the wording of your post, your desired image and your links accordingly.


facebook.request( "me/feed", "POST", attachment )

elseif ( "request" == event.type ) then
local response = event.response
if ( not event.isError ) then
response = json.decode( event.response )
printTable( response, "friends", 3 )
end
print( response )
elseif ( "dialog" == event.type ) then
print( "dialog", event.response )
end
end
-- The above is taken directly from Corona and while I will admit I don't fully understand it, it works, so it's what I'm passing onto you guys.

local appId = xxxxxxxxxx768
if ( appId ) then
facebook.login( "xxxxxxxxxxx768", postit )
else
local function onComplete( event )
system.openURL( "http://developers.facebook.com/setup" )
end

-- Note that the app ID is IN QUOTES one time and NOT the other. Do not add or remove quotes, simply replace my ID with your own.

native.showAlert( "Error", "To develop for Facebook Connect, you need to get an application id from Facebook's website.", { "Learn More" }, onComplete )
end
end
end

fbButton:addEventListener("touch", pressfb)

And that’s it!

It isn’t hard, it’s just different from the last version, slightly. On a side note, do not include facebook.lua in your project folder. It is automatically included. I did not understand this originally and it caused many headaches!

I hope this will help you – you can still refer to my old Facebook tutorial (now outdated) for information on how to set things up on the Facebook end of things.

Peach Pellen o<[]=)

Like this post? Subscribe to the monthly newsletter!

 

  8 Responses to “Add Facebook Posting To Your iPhone App”

  1. It never worked in the simulator, no – you just get that message. But implementing Facebook correctly means when you build for device to test your app as we all do a few times during development, you can just test Facebook then.

    If you’re having trouble, email me – I’ll try to help.

    Peach :)

  2. http://developer.anscamobile.com/node/3662
    Yea, i mean looks like it just doesn’t work on anything but iOS, and doesn’t work on the simulator yet.

  3. Did this sort of thing use to work in the Simulator? Or did you test your program by building it and deploying it to an official simulator/emulator/device (android/iPhone)? If this is the case, I’m curious how you test and develop quickly, seems like a pain to have to build in Corona and then run it on a separate emulator each time.

    At least in the latest version of Corona you see facebook.login() not supported on the simulator?

  4. Hello Gaz,

    It appears that Facebook has, once AGAIN, changed.

    Leave it with me, I’ll update this soon – in the meantime if you want an example to copy let me know via email if it’s important to you and I’ll work it out with you :)

    Peach

  5. Hi,

    I’ve followed your new tutorial for adding facebook, and I have set up an application on facebook. When testing the app, it prompts me to enter my logon details for facebook when I click the button I created, but doesn’t post anything to my wall. Should it work using a test build? Do I need to submit the app to the facebook directory before it works or as long as I have the details filled in should it be ok? Sorry for all the questions but I can’t seem to suss it!

    Thanks

  6. Nice Avatar :)

    I don’t know, I’ve never used one before and haven’t been rejected because of it; I should do some research on that though as I’m not certain.

  7. Cool!

    Yup the facebook in folder got me twisting too :)

    Do you have some kind of function to check for internet connection?
    I’ve heard that you have to have that when publishing to app store?

  8. [...] THIS TUTORIAL IS NOW OUTDATED. PLEASE USE THE NEW FACEBOOK TUTORIAL. [...]

 Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Notify me of followup comments via e-mail. You can also subscribe without commenting.