iosfacebookcocos2d-iphone

Best way to implement Facebook API in cocos 2d


I'm trying hard to find a way to implement the Facebook API in cocos 2d, but I was not able to find one.

Anyone may help me?


Solution

  • The best way to do this, is with a singleton model object using something like a "shared instance" function.

    That way, as the app goes about its business and you don't actively need FB, you can leave your session available on the side and call it as follows.

    //create a static void
    
    static FacebookModel *sharedInstance = nil;
    
    //In the model
    +(FacebookModel *) sharedInstance {
    
    if (sharedInstance == nil) {
    <INSTANTIATE FACEBOOK with an FB property>
    <LOG INTO FACEBOOK with that property>
        }
    return sharedInstance;
    }
    
    -(void)selectorWhichPostsToFacebook {
    <USE CODE HERE TO POST TO FB>
    >
    
    //In your view did load for any class that needs it...
    FacebookModel *fM = [FacebookModel sharedInstance];
    
    //then when you need to actually use fb to do something...
    [fM selectorWhichPostsToFB];