2014-03-26 78 views
0

Facebook login插件用於Cordova移動應用程序android and ios。使用Facebook api我需要獲取facebook用戶配置文件數據並獲取記錄的fb用戶的朋友。我可以知道實現我的目標的正確方法是什麼?Facebook登錄使用Cordova for Android和iOS平臺

請幫幫我。任何幫助,將不勝感激

回答

0

爲獲得用戶資料數據,

[FBSession openActiveSessionWithReadPermissions:@[@"email",@"user_location",@"user_birthday",@"user_hometown"] 
            allowLoginUI:YES 
           completionHandler:^(FBSession *session, FBSessionState state, NSError *error) { 

            switch (state) { 
             case FBSessionStateOpen: 
              [[FBRequest requestForMe] startWithCompletionHandler:^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) { 
               if (error) { 

                NSLog(@"error:%@",error); 


               } 
               else 
               {             
                // retrive user's details at here as shown below 
                NSLog(@"FB user first name:%@",user.first_name); 
                NSLog(@"FB user last name:%@",user.last_name); 
                NSLog(@"FB user birthday:%@",user.birthday); 
                NSLog(@"FB user location:%@",user.location); 
                NSLog(@"FB user username:%@",user.username); 
                NSLog(@"FB user gender:%@",[user objectForKey:@"gender"]); 
                NSLog(@"email id:%@",[user objectForKey:@"email"]); 
                NSLog(@"location:%@", [NSString stringWithFormat:@"Location: %@\n\n", 
                     user.location[@"name"]]); 

               } 
              }]; 
              break; 

             case FBSessionStateClosed: 
             case FBSessionStateClosedLoginFailed: 
              [FBSession.activeSession closeAndClearTokenInformation]; 
              break; 

             default: 
              break; 
            } 

           } ]; 

爲了得到好友列表

- (IBAction)friendsAction:(id)sender 
    { 
     if (!FBSession.activeSession.isOpen) { 
      // if the session is closed, then we open it here, and establish a handler for state changes 
      [FBSession openActiveSessionWithReadPermissions:nil 
               allowLoginUI:YES 
              completionHandler:^(FBSession *session, 
                   FBSessionState state, 
                   NSError *error) { 
               if (error) { 
                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@" friends process cancelled" 
                             message:nil 
                            delegate:nil 
                          cancelButtonTitle:@"OK" 
                          otherButtonTitles:nil]; 
                [alertView show]; 
               } else if (session.isOpen) { 
                [self InviteAction:sender]; 
               } 
              }]; 
      return; 
     } 

     if (self.friendPickerController == nil) { 
      // Create friend picker, and get data loaded into it. 
      self.friendPickerController = [[FBFriendPickerViewController alloc] init]; 
      self.friendPickerController.title = @"Pick Friends"; 
      self.friendPickerController.delegate = self; 
     } 

     [self.friendPickerController loadData]; 
     [self.friendPickerController clearSelection]; 

     [self presentViewController:self.friendPickerController animated:YES completion:nil]; 
    } 

    - (void) performPublishAction:(void (^)(void)) action 
    { 
     if ([FBSession.activeSession.permissions indexOfObject:@"publish_actions"] == NSNotFound) 
     { 
      [FBSession.activeSession requestNewPublishPermissions:@[@"publish_actions"] 
                defaultAudience:FBSessionDefaultAudienceFriends 
               completionHandler:^(FBSession *session, NSError *error) { 
                if (!error) { 
                 action(); 
                } else if (error.fberrorCategory != FBErrorCategoryUserCancelled){ 
                 UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Permission denied" 
                              message:@"Unable to get permission to post" 
                              delegate:nil 
                            cancelButtonTitle:@"OK" 
                            otherButtonTitles:nil]; 
                 [alertView show]; 
                } 
               }]; 
     } else { 
      action(); 
     } 

    } 



    - (void)loginViewFetchedUserInfo:(FBLoginView *)loginView 
           user:(id<FBGraphUser>)user 
    { 
     self.loggedInUser = user; 
    } 


    - (void)facebookViewControllerDoneWasPressed:(id)sender 
    { 

    } 

    - (void)facebookViewControllerCancelWasPressed:(id)sender { 
      } 
+0

感謝您的答覆。我不知道客觀的c。我正在開發使用科爾多瓦3.3。我跟着這個鏈接http://javacourseblog.blogspot.in/2014/01/facebook-login-logout-using-cordova-330.html訪問FB api。 FB插件從https://github.com/phonegap/phonegap-facebook-plugin取出。它在Android中工作正常。但對於ios,它不清楚我應該怎麼做? –