Iphone facebook connect simple integration and example
Yesterday I shared my
helper class on paginating data.
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;