Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Facebook SDK - Best Practices?

I was wondering what the "best practice" guidelines are for using the Facebook iOS SDK.

Specifically - I have several forms that use the Facebook SDK. My initial instinct as an OOP programmer was to create a "FacebookManager" class under my data-access-layer which handles all Facebook activity. Unfortunately since Facebook iOS SDK uses delegates and async methods, this is not so helpful since any other module using FacebookManager would have to pass in delegates for all responses anyway.

Then I figured that each ViewController would be a FBRequestDelegate by itself and handle all Facebook responses.

So, how do you do it in your app?

like image 387
Roey Lehman Avatar asked Mar 02 '12 15:03

Roey Lehman


People also ask

What is Facebook SDK for IOS?

The Facebook SDK enables: Facebook Login - Authenticate people with their Facebook credentials. Share and Send dialogs - Enable sharing content from your app to Facebook. App Events - Log events in your application.

Do I need Facebook SDK to run ads?

Can I run mobile app ads without using the SDK? Yes, we allow anyone to run mobile app install ads from our Ads Create Tool simply by dropping the link to their Apple App Store or Google Play URL. For mobile app engagement ads, you will need to register your app, but can also run ads without the SDK.

What data does Facebook SDK collect?

What data does Facebook collect via the SDK? The Facebook SDK logs the following information: Explicit events - information from events that the advertiser explicitly configures their app to send, such as 'AddtoCart' or 'logPurchase', along with any additional parameters provided.


1 Answers

What I've done is to create a FacebookManager as you suggested. This manager becomes the FBRequestDelegate. Now I prefer blocks to the delegation model so all my FacebookManager methods are block based.

Here is what my post to a user's wall method looks like

- (void)postToUserWall:(NSDictionary*)postDictionary withCompleteHandler:(void(^)(id result, NSError* error))block {


  if( [facebook_ isSessionValid] ) {
    NSMutableDictionary* params = [postDictionary mutableCopy];
    [params setObject:facebook_.accessToken forKey:@"access_token"];
    FBRequest* request = [facebook_ requestWithGraphPath:@"me/feed" andParams:[postDictionary mutableCopy] andHttpMethod:@"POST" andDelegate:self];
    [[request params] setObject:block forKey:@"result"];
  }

}

Then on a success or error I just execute the block

- (void)processRequest:(FBRequest*)request withResult:(id)result andError:(NSError*)error {
  NSDictionary* params = [request params];
  if( [request params] != nil && [params objectForKey:@"result"] != nil ) {
    void(^completeBlock)(id result,NSError* error) = [params objectForKey:@"result"];
    completeBlock(result, nil);
  }
}


- (void)request:(FBRequest *)request didLoad:(id)result {
  [self processRequest:request withResult:result andError:nil];
}


- (void)request:(FBRequest *)request didFailWithError:(NSError *)error {
  [self processRequest:request withResult:nil andError:error];
}

I wish there was a better way to deal with the block but that's the best one I have found so far. Is this the best way to do it? Probably not but that's what I currently use and it seems to be ok, keeping all the FB interaction away from my View Controllers.

Note all the code here assumes ARC that's why there are no releases

like image 87
ribeto Avatar answered Oct 01 '22 16:10

ribeto