需要从"friendUsers"数组中提取Facebook ID,并在Facebook上查询其姓名或个人资料图片



我用了一种迂回的方式。我正在拉我的应用程序的当前用户以及当前用户的Facebook好友。

当你打印friendUsers[这是一个数组]时,你会得到这个:

"<PFUser:1234567:(null)> {n    fbId = abcdefg;n    username = ausername;n}"
)

为了简单起见,我显然换掉了一些信息,但是你明白了。

因此,如果我有一个包含许多用户的数组,我将想要拉出fbId,并查询facebook并获取他们的个人资料照片以显示在表中。然后你可以点击他们的头像。这样做的目的是改变对象的分配对象。

我得到一个不兼容的指针整数转换发送NSIndexPath强到参数类型的NSUInteger在didSelectRowAtIndexPath

不仅如此,我还意识到这里我只选择了一个用户:

PFUser *facebookID = [[PFUser alloc] init];
    facebookID = [friendUsers objectAtIndex:0];

这是因为目前我只有一个其他用户安装了我的应用程序。我只是这样做,看看我的应用程序是否工作时查询Facebook。但是,如果facebook有很多用户,我该如何抓住所有的facebook用户呢?

#import "FacebookFriendViewController.h"
#import "MainQueryViewController.h"
#import <Parse/Parse.h>
@interface FacebookFriendViewController ()
@end
@implementation FacebookFriendViewController
@synthesize friendUsers;
@synthesize pic;
@synthesize object;
- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}
- (void)viewDidLoad
{
    [super viewDidLoad];

    NSLog(@"This is what was passed to the view controller.");
    NSLog(@"Printing friendUsers: %@", friendUsers);
    NSLog(@"%@", object);
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"The count is... count=%d",[friendUsers count]);
    // Return the number of rows in the section.
    return [friendUsers count];

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"friendCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    PFUser *facebookID = [[PFUser alloc] init];
    facebookID = [friendUsers objectAtIndex:0];
    NSString *fbIdString = [[NSString alloc] init];
    fbIdString = [facebookID objectForKey:@"fbId"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    NSLog(@"This is a facebook ID:%@", fbIdString);
    NSURL *profilePictureURL = [NSURL URLWithString:[NSString stringWithFormat:@"https://graph.facebook.com/%@/picture?type=large", fbIdString]];
    NSData *picData = [NSData dataWithContentsOfURL:profilePictureURL];
    UIImageView *fbPic = (UIImageView *)[self.view viewWithTag:1001];
    [fbPic setImage:[UIImage imageWithData:picData]];

    // Configure the cell...
    return cell;
}

#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    [object setObject:[friendUsers objectAtIndex:indexPath] forKey:@"assignedTo"];
}
@end

谢谢你的帮助

根据HRM的查询,以下是发送到好友选择器视图的内容。

 PF_FBRequest *request = [PF_FBRequest requestForMyFriends];
        [request startWithCompletionHandler:^(PF_FBRequestConnection *connection,
                                              id result,
                                              NSError *error) {
            if (!error) {
                // result will contain an array with your user's friends in the "data" key
                NSArray *friendObjects = [result objectForKey:@"data"];
                NSMutableArray *friendIds = [NSMutableArray arrayWithCapacity:friendObjects.count];
                // Create a list of friends' Facebook IDs
                for (NSDictionary *friendObject in friendObjects) {
                    [friendIds addObject:[friendObject objectForKey:@"id"]];
                }
                // Construct a PFUser query that will find friends whose facebook ids
                // are contained in the current user's friend list.
                PFQuery *friendQuery = [PFUser query];
                [friendQuery whereKey:@"fbId" containedIn:friendIds];
                // findObjects will return a list of PFUsers that are friends
                // with the current user
                NSArray *friendUsers = [friendQuery findObjects];

                NSLog(@"%@", friendUsers);

                [self performSegueWithIdentifier:@"sendToFriend" sender:friendUsers];
            }
        }];

要修复不兼容的指针问题,您必须将indexPath.row传递给cellForRowAtIndexPath。

你可以很容易地获取fb用户名:facebookID = [friendUsers objectAtIndex:indexPath.row];在cellForRowAtIndexPath方法。

最新更新