Login to Facebook using FBGraph

前端 未结 1 922
名媛妹妹
名媛妹妹 2021-01-15 21:11

I am new in iOS development. I want to connect to Facebook from my iPhone app. I followed FBGraph API to see how we can use Facebook in our App like:

Prints the inf

相关标签:
1条回答
  • 2021-01-15 21:48

    On click of button and its event is say fbLogin then add this code for login

    -(void)fbLogin
    {
       if(!self.fbGraph.accesstoken) // doesnot have access token. So needed to login
       {
        NSString *client_id = @"130902823636657"; //get your own client id from facebook
    
        //alloc and initalize our FbGraph instance
        self.fbGraph = [[FbGraph alloc] initWithFbClientID:client_id];
    
        //begin the authentication process.....
        [self.fbGraph authenticateUserWithCallbackObject:self andSelector:@selector(fbGraphCallback:) andExtendedPermissions:@"user_photos,user_videos,publish_stream,offline_access,user_checkins,friends_checkins"];
       }
       else
       {
          // Add UIAlert as user is logged in already
          //pop a message letting them know most of the info will be dumped in the log
          UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Note" message:@"user is logged in already" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
          [alert show];
          [alert release];
       }
    }
    

    Now when user is authebticated this method below is called. So add this method in your .h file.

    #pragma mark -
    #pragma mark FbGraph Callback Function
    /**
    * This function is called by FbGraph after it's finished the authentication process
    **/
    - (void)fbGraphCallback:(id)sender 
    {
    
     if ( (self.fbGraph.accessToken == nil) || ([self.fbGraph.accessToken length] == 0) ) 
     {
    
      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Note" message:@"Try Again" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
      [alert show];
      [alert release];
    
      //restart the authentication process.....
      //[self.fbGraph authenticateUserWithCallbackObject:self andSelector:@selector(fbGraphCallback:) andExtendedPermissions:@"user_photos,user_videos,publish_stream,offline_access,user_checkins,friends_checkins"];
    
     } 
     else 
     {
      //pop a message letting them know most of the info will be dumped in the log
      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Note" message:@"Logged In Successfully" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
      [alert show];
      [alert release];
      NSLog(@"------------>CONGRATULATIONS<------------, You're logged into Facebook... Your oAuth token is: %@", self.fbGraph.accessToken);
     }
    }
    
    0 讨论(0)
提交回复
热议问题