Today I will share how I use facebook connect to use in iPhone apps.
Problem of the approach described in facebook conenct website:
1. need to change the project settings include path
2. need to write lots of code to show login prompt, publish code, ask permission , update status etc.
Though these does not seem to be problematic but it really kills time if you are developing quite a few apps and integrating facebook connect into them.
I just encapsulated all the reusable code into a single class and a protocol for required callbacks like what should be done if logged in/out, facebook username fetched by query etc.
For example, say I want to update status.
1. first I need to write code to show login prompt
2. then i need to write code to show permission dialog
3. and then i need to write the code to update user facebook status.
but using facebook agent it is just one line of code!
Code:
// fbAgent is an instacne of FacebookAgent
[fbAgent setStatus:@"My new status"]
for publishing feed there are many methods, one is:
Code:
/**
* Let the agent make attachement for you. You just pass the information
*
*/
- (void) publishFeedWithName:(NSString*)name
captionText:(NSString*)caption
imageurl:(NSString*)url
linkurl:(NSString*)href
userMessagePrompt:(NSString*)prompt;
FacebookAgent will check if the user is logged in, if not it will show the login in prompt.
Once logged in, it will show the permission dialog if not given permission earlier.
Then it will update the user status.
I can also define some delegate method to work on some events like:
Code:
/**
* This method is called if after login or logout
*/
- (void) facebookAgent:(FacebookAgent*)agent loginStatus:(BOOL) loggedIn;
/**
* Must define this method if uploadPhoto is called
*
* This method is called after photo is uploaded
*/
- (void) facebookAgent:(FacebookAgent*)agent photoUploaded:(NSString*) pid;
....
//etc
More detail with sample code is available in this page:
All code is free to use without any obligation even for closed source commercial projects but I will appreciate if you let me know.
**** UPDATE 8 Jan 2010 ***
New functionality added.
Now it is possible to post a photo and then change the status automatically by a single call.
Basically it was scotopia's idea. Thanks.
@scotopia
Code:
- (void) uploadPhotoAtURL:(NSString*)imageurl withCaption:(NSString*)captionOrNil toAlbum:(NSString*)aidOrNil;
- (void) uploadPhotoAsData:(NSData*)imagedata withCaption:(NSString*)captionOrNil toAlbum:(NSString*)aidOrNil;
/**
* Upload photo with status
*/
- (void) uploadPhoto:(NSString*)imageurl withStatus:(NSString*)status;
- (void) uploadPhotoAtURL:(NSString*)imageurl withStatus:(NSString*)status caption:(NSString*)captionOrNil toAlbum:(NSString*)aidOrNil;
- (void) uploadPhotoAsData:(NSData*)imagedata withStatus:(NSString*)status caption:(NSString*)captionOrNil toAlbum:(NSString*)aidOrNil;
/**
* Ask permission to auto approve uploaded photo and add photo to an album.
*/
- (void) askUplaoadPhotoToAlbumPermission;
Today I am will share how my use facebook connect to use in iPhone apps.
Problem of the approach described in facebook conenct website:
1. need to change the project settings include path
2. need to write lots of code to show login prompt, publish code, ask permission , update status etc.
Though these does not seem to be problematic but it really kills time if you are developing quite a few apps and integrating facebook connect into them.
I just encapsulated all the reusable code into a single class and a protocol for required callbacks like what should be done if logged in/out, facebook username fetched by query etc.
For example, say I want to update status.
1. first I need to write code to show login prompt
2. then i need to write code to show permission dialog
3. and then i need to write the code to update user facebook status.
but using facebook agent it is just one line of code!
Code:
// fbAgent is an instacne of FacebookAgent
[fbAgent setStatus:@"My new status"]
for publishing feed there are many methods, one is:
Code:
/**
* Let the agent make attachement for you. You just pass the information
*
*/
- (void) publishFeedWithName:(NSString*)name
captionText:(NSString*)caption
imageurl:(NSString*)url
linkurl:(NSString*)href
userMessagePrompt:(NSString*)prompt;
FacebookAgent will check if the user is logged in, if not it will show the login in prompt.
Once logged in, it will show the permission dialog if not given permission earlier.
Then it will update the user status.
I can also define some delegate method to work on some events like:
Code:
/**
* This method is called if after login or logout
*/
- (void) facebookAgent:(FacebookAgent*)agent loginStatus:(BOOL) loggedIn;
/**
* Must define this method if uploadPhoto is called
*
* This method is called after photo is uploaded
*/
- (void) facebookAgent:(FacebookAgent*)agent photoUploaded:(NSString*) pid;
....
//etc
More detail with sample code is available in this page:
All code is free to use without any obligation even for closed source commercial projects but I will appreciate if you let me know.
Hi Aman.. thanks a lot for this.. i have not tried it.. but sure will be useful. Im giving it a try soon.
Looks very interesting; thanks for the post! I just have a few quick questions
1) Is this already set up to test reachability type stuff or do we need to tweak that ourselves (easy, just curious)
2) Will the feed post method you have work for a situation like this:
In my app there is a user created image gallery and I would like them to be able to upload a select image to facebook and have the status update be something like:
"[uploaded pic here]: I just made this with iPhone App X"
etc.
The previous discussion on this matter lead to that this process would actually be two steps; first uploading the pic to the users unapproved gallery and then making a feed post that includes the image....would your method do it all in one shot or do similar shenanigans need to occur?
@scotopia,
1. no. reachability files often come with some ad network sdk(at least the ones that i/we use for our comes), so to avoid errors its not added in the facebookagent files.
Now, if there is no net and if you've defined
this delegate method, this method will be called with loggedIn = NO.
2. No, but I really like the idea. Hope to add it soon in the FacebookAgent, it will be really handy!
There is also some other similar utility component like that I made for my personal ease like TwitterAgent(no openoath, offers login and message option using UIAlertVews with character counter), HttpPoster/Getter( easy post mixed data like string, int, image, option to fetch dictionary, array etc from json web data), RatingAgent, CommentAgent etc.
But they are yet mostly wired up in the apps.. I will soon separate them in modules and share..
Though I know these are very trivial, no extra coding expertise needed... just need some time... even though it may come into anyone's help... at least mine
Lol okay; I'll stick with the one I'm rolling for right now but I may just be using your stuff in the future! On a related note I'd be interested in seeing the twitter code you just mentioned; I have a version working right now where I'm uploading to twitpic/posting the link to twitter but its very raw; currently hardcoding my twitter username/password as a test and not doing any nice login control flow etc.
Working on integrating right now; seems pretty streamlined! One question though; the session doesn't seem to be persisting when I exit and start the app up again; doesn't there need to be some kind of [session resume] call somewhere? I'm in a rush and doing this hastily so I may have missed something; thanks!
doesn't do exactly what I was suggesting earlier. The status update and the picture upload do happen in one call, yes, but they are unrelated. What would really be awesome is to have the status update link to the uploaded image as I was stating above.
Perhaps have something where the photoUploaded callback triggers the status update using the pID to somehow include the image or some such? Fiddling with it now but still new to this facebook jazz.
True; is there any benefit to having it be a "double wammy" and going to a status update as well? I was under the assumption that it would be more visible that way? Basically I want to reproduce what the native facebook apps like farmville do: dynamic image, text, app icon, link to the app etc:
So this can not take imageWithData right from the app correct; so the image must first be uploaded using previous techniques and then linked by the url? (I am at my day job right now and not on a mac so I'm currently flying blind).
So this can not take imageWithData right from the app correct; so the image must first be uploaded using previous techniques and then linked by the url? (I am at my day job right now and not on a mac so I'm currently flying blind).
Thanks for all your hard work so far!
you r right, in this case a url not imagedata has to be provided.
I am under lees pressure at my job at the moment ... just utilizing my time by modularizing the scattered code
alternatively, you can also upload an image to your own server and use the image url to publish the feed. it will also help in another situation( if ever occurs/possible), have an admin panel to delete unwanted/BAD images (which often become a serious issue for photo uploading apps).
True; I'd rather have the users upload the photos to to their own facebook instead of clogging up my server with billions of images You can always manually delete your own facebook photos if needed.
but, then the photos will be added to an album named after the facebook app name for each user... and AFAIK an album has an maximum photo limit of 200 or something, right?
That's correct; but I don't think that's really a problem. If they've blasted out that many advertisements for my app thats a problem I'm willing to have!