Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

Simple way to integrate facebook. reused & reduced code.One line code to do actions!

AmanAppsAmanApps Posts: 61Registered Users
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!

// fbAgent is an instacne of FacebookAgent
[fbAgent setStatus:@\"My new status\"]



for publishing feed there are many methods, one is:

/**
* 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:


/**
* 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


- (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;

Post edited by AmanApps on
iOS application development lead at MobiLab Solutions.

Expert in Java/Spring/OSGi, iOS app development.

Enthusiastic C/C++, PHP, Cocoa Mac App developer.
«1345

Replies

  • rocotilosrocotilos Posts: 3,216iPhone Dev SDK Supporter, Registered Users
    AmanApps;158585 said:
    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!

    // fbAgent is an instacne of FacebookAgent
    [fbAgent setStatus:@\"My new status\"]



    for publishing feed there are many methods, one is:

    /**
    * 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:


    /**
    * 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.
  • AmanAppsAmanApps Posts: 61Registered Users
    Thanks rocotilos, its really an inspiration.
    iOS application development lead at MobiLab Solutions.

    Expert in Java/Spring/OSGi, iOS app development.

    Enthusiastic C/C++, PHP, Cocoa Mac App developer.
  • scotopiascotopia Posts: 2,072Registered Users
    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!
  • AmanAppsAmanApps Posts: 61Registered Users
    @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

    - (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!
    iOS application development lead at MobiLab Solutions.

    Expert in Java/Spring/OSGi, iOS app development.

    Enthusiastic C/C++, PHP, Cocoa Mac App developer.
  • scotopiascotopia Posts: 2,072Registered Users
    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.
  • AmanAppsAmanApps Posts: 61Registered Users
    @scotopia

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


    - (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!
    iOS application development lead at MobiLab Solutions.

    Expert in Java/Spring/OSGi, iOS app development.

    Enthusiastic C/C++, PHP, Cocoa Mac App developer.
  • rarindeedrarindeed Posts: 271Registered Users
    Very, very cool! Thanks for sharing!
    @rarindeed





    Burstly is the only open and free ad management platform for iPhone app developers.
  • scotopiascotopia Posts: 2,072Registered Users
    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!
  • scotopiascotopia Posts: 2,072Registered Users
    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:



    -(void) resumeSession{
    [_session resume];
    }

  • scotopiascotopia Posts: 2,072Registered Users
    I noticed something else:


    - (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.
  • AmanAppsAmanApps Posts: 61Registered Users
    @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/index.php/Photos.get

    btw, when a user adds a photo, that is published as feed to the wall by facebook itself.
    iOS application development lead at MobiLab Solutions.

    Expert in Java/Spring/OSGi, iOS app development.

    Enthusiastic C/C++, PHP, Cocoa Mac App developer.
  • scotopiascotopia Posts: 2,072Registered Users
    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:

    image
  • AmanAppsAmanApps Posts: 61Registered Users
    For this you need to publish a feed with action links. In short, the attachment string should has the following segment:


    \"\\"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
    iOS application development lead at MobiLab Solutions.

    Expert in Java/Spring/OSGi, iOS app development.

    Enthusiastic C/C++, PHP, Cocoa Mac App developer.
  • scotopiascotopia Posts: 2,072Registered Users
    Ahh I see; I'm reading into it all now but you'll likely beat me to the punch!
  • AmanAppsAmanApps Posts: 61Registered Users
    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.
    iOS application development lead at MobiLab Solutions.

    Expert in Java/Spring/OSGi, iOS app development.

    Enthusiastic C/C++, PHP, Cocoa Mac App developer.
  • AmanAppsAmanApps Posts: 61Registered Users
    @scotopia: Added new some more functions. Now action link can be added in feed.

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

    :)
    iOS application development lead at MobiLab Solutions.

    Expert in Java/Spring/OSGi, iOS app development.

    Enthusiastic C/C++, PHP, Cocoa Mac App developer.
  • scotopiascotopia Posts: 2,072Registered Users
    AmanApps;159084 said:
    @scotopia: Added new some more functions. Now action link can be added in feed.

    [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!
  • AmanAppsAmanApps Posts: 61Registered Users
    scotopia;159162 said:
    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 :D
    iOS application development lead at MobiLab Solutions.

    Expert in Java/Spring/OSGi, iOS app development.

    Enthusiastic C/C++, PHP, Cocoa Mac App developer.
  • scotopiascotopia Posts: 2,072Registered Users
    AmanApps;159168 said:
    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 :D
    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!
  • AmanAppsAmanApps Posts: 61Registered Users
    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).
    iOS application development lead at MobiLab Solutions.

    Expert in Java/Spring/OSGi, iOS app development.

    Enthusiastic C/C++, PHP, Cocoa Mac App developer.
  • scotopiascotopia Posts: 2,072Registered Users
    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.
  • AmanAppsAmanApps Posts: 61Registered Users
    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/facebook-album-uploads/
    iOS application development lead at MobiLab Solutions.

    Expert in Java/Spring/OSGi, iOS app development.

    Enthusiastic C/C++, PHP, Cocoa Mac App developer.
  • scotopiascotopia Posts: 2,072Registered Users
    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!
  • scotopiascotopia Posts: 2,072Registered Users
    I got it working extremely close to what I need; just have a few questions.

    1. When the image gets uploaded it automatically goes to the feed as well; so with that and the deliberate feed post it is a "double whammy" that might be annoying; is there anyway to make the image upload go ninja style; so that it doesn't trigger a feed post?

    2. Is there anyway to make it so the user is not prompted to insert some text before the feed publish? I tried setting the userPrompt field to nil but it just seemed to default it to "what's on your mind" with the the text area still there. I'm reading the docs now but its confusing: I think some auto_publish bool must be set to true somewhere and the app must have "extended permission"

    Edit: I figured this one out: the "preview" value must be set to 0 in the feedDialogue; for some reason permissions didn't need to to be asked for publish stream; weird...

    Also one suggestion: on the photo upload methods you have one of the parameters is captionOrNil; this is a little misleading as a nil causes a crash; perhaps a captionOrEmptyString ?
Sign In or Register to comment.