title: Facebook SDK integration - using a view controller instead of the app delegate
body: <p>As the title suggests, I now find myself utilising the new Facebook SDK--available <a href="https://github.com/facebook/facebook-ios-sdk" rel="nofollow">here</a>-- within my iOS application. </p>
  
  <p>I've followed <a href="https://developers.facebook.com/docs/mobile/ios/build/" rel="nofollow">this</a> introduction outlined on the Facebook developer site, and everything works as it should--at least according to their implementation. </p>
  
  <p>Their way of doing things means the user is presented with the choice to connect their account to the app after applicationDidFinishLaunching is called, via a series of methods and calls within the app delegate. </p>
  
  <p>I would instead like to only force this upon the selection of a button within a view controller buried elsewhere in the app. That is, I don't want to deal with authorisation when applicationDidFinishLaunching is called--I want my own method in a view controller to force it. Also, I will be referencing the Facebook classes from a number of my own view controllers, if that makes any difference.</p>
  
  <p>I've searched far and wide and have yet to come up with a fully explained solution (or anything at all, really) for dealing with this. If anyone has ideas about how to go about this, please let me know. StackOverflow is a last resort most of the time :) </p>
  
  <p>Edit: As beryllium brought up below, I can simply take everything that is meant to be in the app delegate and slap it into each individual view controller. It works, but is this the best way to handle this unique situation or is there a better implementation to be had?</p>
  
  <p>Thank you,</p>
  
  <p>Relevant code below:</p>
  
  <p>sampleAppDelegate.h</p>
  
  <pre><code>@interface sampleAppDelegate : NSObject &lt;UIApplicationDelegate,FBRequestDelegate, FBSessionDelegate, FBDialogDelegate, FBLoginDialogDelegate&gt; {
  
  Facebook *facebook;
  }
  
  @property (nonatomic, retain) Facebook *facebook;
  
  @end
  </code></pre>
  
  <p>sampleAppDelegate.m</p>
  
  <pre><code>  @synthesize facebook;
  
        facebook = [[Facebook alloc] initWithAppId:@"102698856525645" andDelegate:self];
  
          NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
          if ([defaults objectForKey:@"FBAccessTokenKey"] 
              &amp;&amp; [defaults objectForKey:@"FBExpirationDateKey"]) {
              facebook.accessToken = [defaults objectForKey:@"FBAccessTokenKey"];
              facebook.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"];
          }
  
          if (![facebook isSessionValid]) {
              NSArray *permissions = [[NSArray alloc] initWithObjects:
                                      @"offline_access", 
                                      @"publish_stream",
                                      nil];
              [facebook authorize:permissions];
              [permissions release];   
          } 
      }
      // For 4.2+ support
      - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
        sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
          return [facebook handleOpenURL:url]; 
      }
  
      - (void)fbDidLogin {
          NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
          [defaults setObject:[facebook accessToken] forKey:@"FBAccessTokenKey"];
          [defaults setObject:[facebook expirationDate] forKey:@"FBExpirationDateKey"];
          [defaults synchronize];
  
      } 
  /*methods required by Facebook SDK to handle issues with connectivity--all currently empty*/
  </code></pre>
  
  <p>viewController.h</p>
  
  <pre><code>#import "FBConnect.h"
  #import "sampleAppDelegate.h"
  
  @class sampleAppDelegate;
  
  
  @interface viewController : UIViewController &lt;FBRequestDelegate&gt; {
      Facebook *facebook;
          UnitedUpdatesAppDelegate *facebookSampleAppDelegate;
  
  
  }
  @property (nonatomic, retain) sampleAppDelegate *appDelegate;
  @property (nonatomic, retain) Facebook *facebook;
  </code></pre>
  
  <p>viewController.m</p>
  
  <pre><code>-(void) facebookPost {
      NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
  
                                     @"http://stackoverflow.com", @"link",
                                     @"Message name", @"name",
                                     @"Caption", @"caption",
                                     @"Description", @"description",
                                     @"Message",  @"message",
                                     nil];
  
      [sampleAppDelegate dialog:@"feed" andParams:params andDelegate: sampleAppDelegate];
  }
  </code></pre>