Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Obj-C - Facebook API - Access An Instance?

  • 14-04-2011 9:31am
    #1
    Registered Users, Registered Users 2 Posts: 8,004 ✭✭✭


    Hi Folks,

    I'm probably just missing something but I'm trying to implement the FaceBook API into my App.

    In my AppDelegate I follow the given tutorial from FaceBook:

    .h File
    Facebook *facebook;
    
    @property (nonatomic, retain) Facebook *facebook;
    
    

    .m File
    //Did Finish Launching
    
    //Facebook 
    	
    facebook = [[Facebook alloc] initWithAppId:@"APP_ID_XXXXX"];
    
     [facebook authorize:nil delegate:self];
    


    Now, this works perfectly. I get the Sign In and Authorise working just fine. The problem is, how do I access the "facebook" instance in say "AnotherClass.m" where I might want to do a Status update etc?
    //Status Method - In a different class to App Delegate
    
    NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
    								   
    @"http://www.deadmau5.com", @"link",
    @"Facebook Dialogs From My App",  @"message", nil];
    	
    [facebook dialog:@"feed" andParams:params andDelegate:self];
    

    This again works, but I have to alloc a Facebook object. Surely can I not use the one in the Delegate?

    I've tried a +(void) updateStatus type function in the AppDelegate but as expected, it doesn't work. I get warnings regarding variables in class methods.

    Any help greatly appreciated!


Comments

  • Registered Users, Registered Users 2 Posts: 981 ✭✭✭fasty


    Easy way:
    static Facebook *gFacebook = NIL;
    
    + (Facebook *)facebookInstance
    {
       if (gFacebook == NIL)
          gFacebook = [[Facebook alloc] initWithWhateverYouWantHere];
       }
       
       return gFacebook;
    }
    

    But since the above is a global, you may as well declare it
    extern Facebook* facebook;
    

    More info here:

    http://cocoawithlove.com/2008/11/singletons-appdelegates-and-top-level.html

    The right way, imo is to pass dependencies to the objects that use them. In your app delegate, have facebook object as before, but pass said object to the classes/controllers that need it. I usually make custom init functions for this purpose.

    I'm not saying don't use globals, but they can lead to problems if you're not careful and definitely are not thread safe. For something like a facebook connection you're probably all right.


  • Registered Users, Registered Users 2 Posts: 8,004 ✭✭✭ironclaw


    Cheers for the reply. With relation to this:
    // AppDelegate.h
    
    static Facebook *gFacebook = NIL;
    
    + (Facebook *)facebookInstance
    {
       if (gFacebook == NIL)
          gFacebook = [[Facebook alloc] initWithWhateverYouWantHere];
       }
       
       return gFacebook;
    }
    

    How would I call this and use it? For example in "AnotherClass.m"? I've never returned a instance like this.
    //include AppDelegate.h
    
    facebook = [AppDelegate facebookInstance] //Get instance?
    
    //Use it
    
    [facebook dialog:@"feed" andParams:params andDelegate:self];
    


  • Registered Users, Registered Users 2 Posts: 981 ✭✭✭fasty


    Something like this should do it. All this code is me typing off the top of my head in a language/API I don't always use so no promised that it'll compile or anything! I can help with errors though!

    Apple's UIApplication class has a class method called sharedApplication. It gets the global app instance. You can then access the delegate property like so:
    Facebook *instance = [[UIApplication sharedApplication].delegate facebookInstance]
    

    So put the function I originally posted into your app delegate class and see how you get on!


Advertisement