Advertise Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

sdkIQ for iPhone
($4.99)

Your First iPhone App
($1.99)

iPhone Code Generator
($9.99)

Dual Matches
($0.99)

Calcuccino Programmers' Calculator
($2.99)

SDKtoday
(free)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Tools & Utilities

Reply
 
LinkBack Thread Tools Display Modes
Old 01-06-2010, 03:47 PM   #1 (permalink)
iPhone Developer, PlayDom
 
AmanApps's Avatar
 
Join Date: Jul 2009
Posts: 61
Post Simple way to integrate facebook. reused & reduced code.One line code to do actions!

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;

Last edited by AmanApps; 02-17-2010 at 06:34 AM.
AmanApps is offline   Reply With Quote
Old 01-06-2010, 11:22 PM   #2 (permalink)
iPod Touch 8GB
 
rocotilos's Avatar
 
Join Date: Oct 2009
Location: MY
Age: 32
Posts: 1,604
Default

Quote:
Originally Posted by AmanApps View Post
Yesterday I shared my helper class on paginating data.

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.
__________________

New & Noteworthy Apr '10
(click icon.. it's a FREE App!)

"...I decided that Apple can't afford to change its core values and simply let it slide. We have the same core values as when we started, and we come into work wanting to do the same thing today that we wanted to do five years ago."
rocotilos is offline   Reply With Quote
Old 01-06-2010, 11:40 PM   #3 (permalink)
iPhone Developer, PlayDom
 
AmanApps's Avatar
 
Join Date: Jul 2009
Posts: 61
Default

Thanks rocotilos, its really an inspiration.
AmanApps is offline   Reply With Quote
Old 01-07-2010, 12:42 PM   #4 (permalink)
Registered Member
 
scotopia's Avatar
 
Join Date: Oct 2008
Posts: 1,931
Default

Hi Aman,

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?

Again; thanks for the post!
scotopia is offline   Reply With Quote
Old 01-07-2010, 01:08 PM   #5 (permalink)
iPhone Developer, PlayDom
 
AmanApps's Avatar
 
Join Date: Jul 2009
Posts: 61
Default

@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
Code:
- (void) facebookAgent:(FacebookAgent*)agent loginStatus:(BOOL) loggedIn;
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

Thanks for you reply!

Last edited by AmanApps; 01-07-2010 at 01:08 PM. Reason: dsf
AmanApps is offline   Reply With Quote
Old 01-07-2010, 01:15 PM   #6 (permalink)
Registered Member
 
scotopia's Avatar
 
Join Date: Oct 2008
Posts: 1,931
Default

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.
scotopia is offline   Reply With Quote
Old 01-07-2010, 02:51 PM   #7 (permalink)
iPhone Developer, PlayDom
 
AmanApps's Avatar
 
Join Date: Jul 2009
Posts: 61
Default

@scotopia

Hey! I've just updated the facebookagent. Now it has few more functions:

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;
So now you can do what you wanted i.e. post a photo and then change the status automatically by a single call!

Last edited by AmanApps; 02-17-2010 at 06:35 AM.
AmanApps is offline   Reply With Quote
Old 01-07-2010, 03:40 PM   #8 (permalink)
Registered Member
 
Join Date: Dec 2009
Posts: 266
Default

Very, very cool! Thanks for sharing!
__________________
@rarindeed


Burstly is the only open and free ad management platform for iPhone app developers.
rarindeed is offline   Reply With Quote
Old 01-07-2010, 05:35 PM   #9 (permalink)
Registered Member
 
scotopia's Avatar
 
Join Date: Oct 2008
Posts: 1,931
Default

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!
scotopia is offline   Reply With Quote
Old 01-07-2010, 10:56 PM   #10 (permalink)
Registered Member
 
scotopia's Avatar
 
Join Date: Oct 2008
Posts: 1,931
Default

Yes; I believe you do need a way to resume; I just added this as a method to your Agent and it's all good:

Code:
-(void) resumeSession{
	[_session resume];
}
scotopia is offline   Reply With Quote
Old 01-07-2010, 11:38 PM   #11 (permalink)
Registered Member
 
scotopia's Avatar
 
Join Date: Oct 2008
Posts: 1,931
Default

I noticed something else:

Code:
- (void) uploadPhotoAsData:(NSData*)imagedata withStatus:(NSString*)status caption:(NSString*)captionOrNil toAlbum:(NSString*)aidOrNil{
	self.newStatus = status;
	[self uploadPhotoAsData:imagedata withCaption:captionOrNil toAlbum:aidOrNil];
}
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.
scotopia is offline   Reply With Quote
Old 01-07-2010, 11:52 PM   #12 (permalink)
iPhone Developer, PlayDom
 
AmanApps's Avatar
 
Join Date: Jul 2009
Posts: 61
Default

@scotopia
yea, resume is not there. thanks.

to get the photo url from id you need to run a fql which is not also in FacebookAgent now.
If you implement I will appreciate if you share

http://wiki.developers.facebook.com/...php/Photos.get

btw, when a user adds a photo, that is published as feed to the wall by facebook itself.
AmanApps is offline   Reply With Quote
Old 01-08-2010, 12:03 AM   #13 (permalink)
Registered Member
 
scotopia's Avatar
 
Join Date: Oct 2008
Posts: 1,931
Default

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:


Last edited by scotopia; 01-08-2010 at 12:07 AM.
scotopia is offline   Reply With Quote
Old 01-08-2010, 12:16 AM   #14 (permalink)
iPhone Developer, PlayDom
 
AmanApps's Avatar
 
Join Date: Jul 2009
Posts: 61
Default

For this you need to publish a feed with action links. In short, the attachment string should has the following segment:

Code:
"\"properties\":{\"Fertilize \":{\"text\":\"Fertilize their corps\",\"href\":\"http://url.to.the/page\"}}
More here and here.
Hope it helps for now.

I will add to to FacebookAgent soon, if you update the class by this time, please share

Thanks
AmanApps is offline   Reply With Quote
Old 01-08-2010, 12:18 AM   #15 (permalink)
Registered Member
 
scotopia's Avatar
 
Join Date: Oct 2008
Posts: 1,931
Default

Ahh I see; I'm reading into it all now but you'll likely beat me to the punch!
scotopia is offline   Reply With Quote
Old 01-08-2010, 12:22 AM   #16 (permalink)
iPhone Developer, PlayDom
 
AmanApps's Avatar
 
Join Date: Jul 2009
Posts: 61
Default

hey! c'mon man! no question of beating! Thats the community is for! I knew only because I used this earlier in my last 12 projects.

Thanks for your support.
AmanApps is offline   Reply With Quote
Old 01-08-2010, 12:24 AM   #17 (permalink)
Registered Member
 
scotopia's Avatar
 
Join Date: Oct 2008
Posts: 1,931
Default

Lol; just a figure of speech, no worries!
scotopia is offline   Reply With Quote
Old 01-08-2010, 05:55 AM   #18 (permalink)
iPhone Developer, PlayDom
 
AmanApps's Avatar
 
Join Date: Jul 2009
Posts: 61
Default

@scotopia: Added new some more functions. Now action link can be added in feed.
Code:
[fbAgent publishFeedWithName:@"Hello world!" 
					 captionText:@"How are you! :)" 
						imageurl:@"http://amanpages.com/wordpress/wp-content/uploads/2009/12/logo2.png"  
						 linkurl:@"http://amanpages.com/"
			   userMessagePrompt:@"Some prompt" 
					 actionLabel:@"Want to know more?"
					  actionText:@"Search Google" 
					  actionLink:@"http://www.google.com"];

Download the code form previous location.

AmanApps is offline   Reply With Quote
Old 01-08-2010, 10:15 AM   #19 (permalink)
Registered Member
 
scotopia's Avatar
 
Join Date: Oct 2008
Posts: 1,931
Default

Quote:
Originally Posted by AmanApps View Post
@scotopia: Added new some more functions. Now action link can be added in feed.
Code:
[fbAgent publishFeedWithName:@"Hello world!" 
					 captionText:@"How are you! :)" 
						imageurl:@"http://amanpages.com/wordpress/wp-content/uploads/2009/12/logo2.png"  
						 linkurl:@"http://amanpages.com/"
			   userMessagePrompt:@"Some prompt" 
					 actionLabel:@"Want to know more?"
					  actionText:@"Search Google" 
					  actionLink:@"http://www.google.com"];

Download the code form previous location.

You are a speed demon!

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!

Last edited by scotopia; 01-08-2010 at 10:20 AM.
scotopia is offline   Reply With Quote
Old 01-08-2010, 10:39 AM   #20 (permalink)
iPhone Developer, PlayDom
 
AmanApps's Avatar
 
Join Date: Jul 2009
Posts: 61
Default

Quote:
Originally Posted by scotopia View Post
You are a speed demon!

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
AmanApps is offline   Reply With Quote
Old 01-08-2010, 10:45 AM   #21 (permalink)
Registered Member
 
scotopia's Avatar
 
Join Date: Oct 2008
Posts: 1,931
Default

Quote:
Originally Posted by AmanApps View Post
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
Lol; okay, so does this sound about right for what I want to accomplish:

1) Do a regular uploadImage (not to feed or anything or else it will be a "double wammy"

2) In the imageUploaded (or whatever) callback do a fql call using the returned pID to get the URL of the image just uploaded

3) Use that url in the method you just added

This will produce a single feed post like the farmville one above with the uploaded image in it, correct? Thanks!
scotopia is offline   Reply With Quote
Old 01-08-2010, 10:58 AM   #22 (permalink)
iPhone Developer, PlayDom
 
AmanApps's Avatar
 
Join Date: Jul 2009
Posts: 61
Default

yea, right.

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).
AmanApps is offline   Reply With Quote
Old 01-08-2010, 11:05 AM   #23 (permalink)
Registered Member
 
scotopia's Avatar
 
Join Date: Oct 2008
Posts: 1,931
Default

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.
scotopia is offline   Reply With Quote
Old 01-08-2010, 11:13 AM   #24 (permalink)
iPhone Developer, PlayDom
 
AmanApps's Avatar
 
Join Date: Jul 2009
Posts: 61
Default

yea

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?

UPDATE:
just now came to know that FB withdrew album photo limit!!!
http://www.allfacebook.com/2009/01/f...album-uploads/
AmanApps is offline   Reply With Quote
Old 01-08-2010, 11:38 AM   #25 (permalink)
Registered Member
 
scotopia's Avatar
 
Join Date: Oct 2008
Posts: 1,931
Default

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!
scotopia is offline   Reply With Quote
Reply

Bookmarks

Tags
facebook, facebook api, facebook connect

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


» Advertisements
» Stats
Members: 41,862
Threads: 49,770
Posts: 213,057
Top Poster: BrianSlick (3,139)
Welcome to our newest member, futurevilla216
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 07:05 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0