1

Facebookの名前と画像を同時に取得するのに問題があります。リクエストごとに1つしか受け取れません。

-(void)fbDidLogin{
    // Save the access token key info.
    [self saveAccessTokenKeyInfo];

    // Get the user's info.
    [facebook requestWithGraphPath:@"me/?fields=first_name,picture" andDelegate:self];

}

-(void)request:(FBRequest *)request didLoad:(id)result{

    if ([result isKindOfClass:[NSArray class]]) {

        result = [result objectAtIndex:0];

        if ([result objectForKey:@"first_name"]) {
            [lblUser setText:[NSString stringWithFormat:@"Welcome %@!", [result objectForKey:@"first_name"]]];
            // Show the publish button.
            [btnPublish setHidden:NO];
        }
    }
    else {
       imageView.image = [UIImage imageWithData:result];
    }      
}

私が得るエラー

__NSCFDictionary length]: unrecognized selector sent to instance
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary length]: unrecognized selector sent to instance 

2つのリクエストでアプローチした場合

-(void)fbDidLogin{
    // Save the access token key info.
    [self saveAccessTokenKeyInfo];

    // Get the user's info.
    [facebook requestWithGraphPath:@"me/picture" andDelegate:self];
    [facebook requestWithGraphPath:@"me" andDelegate:self];
}

同じ-(void)request:(FBRequest *)request didLoad:(id)resultを使用すると、取得するエラーは次のようになります。

[__NSCFDictionary length]: unrecognized selector sent to instance

では、Facebookの名前と画像の取得をリクエストするにはどうすればよいですか?また、受信データを処理するにはどうすればよいですか?

4

2 に答える 2

1

私はそれを修正しました、次のコードはFacebookの名前とプロフィール写真を取得するためにうまく機能します

-(void)request:(FBRequest *)request didLoad:(id)result{
    // With this method we’ll get any Facebook response in the form of an array.
    // In this example the method will be used twice. Once to get the user’s name to
    // when showing the welcome message and next to get the ID of the published post.
    // Inside the result array there the data is stored as a NSDictionary.    


    if ([result isKindOfClass:[NSData class]])
    {
        NSLog(@"Profile Picture");
        imageView.image = [UIImage imageWithData:result];
        //[profilePicture release];
        //profilePicture = [[UIImage alloc] initWithData: result];
    }


  if ([result isKindOfClass:[NSArray class]]) {
        // The first object in the result is the data dictionary.

        result = [result objectAtIndex:0];


    }

    if ([result isKindOfClass:[NSDictionary class]]) {
        // If the current result contains the "first_name" key then it's the user's data that have been returned.
        // Change the lblUser label's text.
        [lblUser setText:[NSString stringWithFormat:@"Welcome %@!", [result objectForKey:@"first_name"]]];
        // Show the publish button.
        [btnPublish setHidden:NO];
    }

}
于 2012-06-21T19:55:27.567 に答える
0

Facebookサーバーからの応答は、配列ではなく1つの辞書です。に関する2行を削除し、NSArray単に使用します。

if ([result objectForKey:@"first_name"]) {
        [lblUser setText:[NSString stringWithFormat:@"Welcome %@!", [result objectForKey:@"first_name"]]];
        NSLog(@"%@", lblUser);
        // Show the publish button.
        [btnPublish setHidden:NO];
}
于 2012-06-21T18:57:13.493 に答える