title: Using Facebook Graph API on iOS
body: <p>This is my first post on StackOverflow but i use this for a long time...</p>
  
  <p>My problem is basically simple, I want to share the content of a <code>UITextView(mShareText)</code> on the user's wall without prompting any Dialog. Basically, the user has to fill its message on the <code>UITextView</code> and then click on a "<kbd>post</kbd>" UIButton. Simple isn't it ?</p>
  
  <p>I've downloaded the Facebook iOS SDK and copied it into my project. I've included "<code>FBConnect.h</code>" and "<code>Facebook.h</code>" on my controller's header file and created a var : <code>Facebook* facebook</code>;, the controller also implements the following delegates : FBSessionDelegate, FBRequestDelegate.</p>
  
  <p>On my controller's implementation file, I've a IBAction triggered when the user clicks on the "post" UIButton.</p>
  
  <pre><code>- (IBAction)onShareByFBPressed:(id)sender {
  NSLog(@"onShareByFBPressed");
  
  // Facebook settings
  NSMutableDictionary* params = [[NSMutableDictionary alloc] initWithCapacity:3];
  
  [params setObject:mShareText.text forKey:@"message"];
  [params setObject:@"http://www.example.com" forKey:@"link"];
  [params setObject:@"https://www.example.com/myImg.jpg" forKey:@"picture"];
  
  [facebook requestWithGraphPath:@"me/feed" 
                           andParams:params
                       andHttpMethod:@"POST"
                         andDelegate:self];
      [params release];
  }
  </code></pre>
  
  <p>I've implemented this on my viewdidload:</p>
  
  <pre><code>- (void)viewDidLoad
  {
      NSLog(@"viewDidLoad");
      [super viewDidLoad];
  
  (...)
  
      // Test facebook
      facebook = [[Facebook alloc] initWithAppId:@"XXXXXXXXXXX" 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"];
          NSLog(@"AccessToken: %@ ExpirationDate: %@", facebook.accessToken, facebook.expirationDate);
      }else{
          NSLog(@"No AccessToken or ExpirationDate");
      }
      if (![facebook isSessionValid]) {
          [facebook authorize:[NSArray arrayWithObjects:@"publish_stream", nil]];
      }
  }
  </code></pre>
  
  <p>Here are different methods I had to implement due to the delegates :</p>
  
  <pre><code>// Pre 4.2 support
  - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
      return [facebook handleOpenURL:url]; 
  }
  
  // For 4.2+ support
  - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
      return [facebook handleOpenURL:url]; 
  }
  
  - (void)fbDidLogin {
      NSLog(@"fbDidLogin");
      NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
      [defaults setObject:[facebook accessToken] forKey:@"FBAccessTokenKey"];
      [defaults setObject:[facebook expirationDate] forKey:@"FBExpirationDateKey"];
      [defaults synchronize];
  }
  
  - (void)fbSessionInvalidated{
      NSLog(@"fbSessionInvalidated");
  }
  
  - (void)fbDidLogout{
      NSLog(@"fbDidLogout");
  }
  
  - (void)fbDidExtendToken:(NSString*)accessToken expiresAt:(NSDate*)expiresAt{
      NSLog(@"fbDidExtendToken");
  }
  
  - (void)fbDidNotLogin:(BOOL)cancelled{
      NSLog(@"fbDidNotLogin");
  }
  
  - (void)request:(FBRequest *)request didReceiveResponse:(NSURLResponse *)response{
      NSLog(@"didReceiveResponse: %@", response);
  }
  
  - (void)request:(FBRequest *)request didFailWithError:(NSError *)error{
      NSLog(@"didFailWithError: %@", [error description]);
      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Share on Facebook" 
                                                  message:@"An error occured" 
                                                 delegate:nil 
                                        cancelButtonTitle:@"OK"
                                        otherButtonTitles:nil];
      [alert show];
      [alert release];
  }
  </code></pre>
  
  <p>Unfortunatelly, everytime I try to trigger my "onShareByFBPressed" method, I got the following error on the log:</p>
  
  <pre><code>2012-02-17 13:02:06.401 Mixtapes[935:707] didFailWithError: Error Domain=facebookErrDomain     Code=10000 "The operation couldn’t be completed. (facebookErrDomain error 10000.)"     UserInfo=0x1f6f60 {error=&lt;CFBasicHash 0x1f6660 [0x3ec6d630]&gt;{type = mutable dict, count = 3,
  entries =&gt;
      2 : &lt;CFString 0x1f64a0 [0x3ec6d630]&gt;{contents = "type"} = &lt;CFString 0x1f6b10     [0x3ec6d630]&gt;{contents = "OAuthException"}
      3 : &lt;CFString 0x1f6ab0 [0x3ec6d630]&gt;{contents = "message"} = &lt;CFString 0x1f6a10     [0x3ec6d630]&gt;{contents = "An active access token must be used to query information about the     current user."}
      6 : &lt;CFString 0x1f6e60 [0x3ec6d630]&gt;{contents = "code"} = 2500
  }
  }
  </code></pre>
  
  <p>Can anyone tell me what's wrong with my implementation ??</p>
  
  <p>Thanks a lot.</p>
  
  <p>Best regards.</p>
  
  <p>EDIT:
  @Adil</p>
  
  <p>Thanks for your proposal. It actually works somethimes. </p>
  
  <p>Sometimes i got the following error : </p>
  
  <pre><code>didFailWithError: Error Domain=facebookErrDomain Code=10000 "The operation couldn’t be     completed. (facebookErrDomain error 10000.)" UserInfo=0xa17e0d0 {error=&lt;CFBasicHash 0xa171e10     [0x3ec6d630]&gt;{type = mutable dict, count = 3,
  entries =&gt;
      2 : &lt;CFString 0xa179d10 [0x3ec6d630]&gt;{contents = "type"} = &lt;CFString 0xa17a280     [0x3ec6d630]&gt;{contents = "OAuthException"}
      3 : &lt;CFString 0x6127710 [0x3ec6d630]&gt;{contents = "message"} = &lt;CFString 0xa178db0     [0x3ec6d630]&gt;{contents = "Error validating access token: Session is invalid. This could be     because the application was uninstalled after the session was created."}
      6 : &lt;CFString 0xa17e020 [0x3ec6d630]&gt;{contents = "code"} = 190
  }
  }
  </code></pre>
  
  <p>Thanks for your help</p>