Получение имени профиля Facebook и изображения IOS 5.1
У меня проблемы с одновременным получением имени и изображения на Facebook. Я могу принять только один из них каждый запрос.
-(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
Если я подойду с двумя запросами
-(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) запрос:(FBRequest *) запрос didLoad:(id) результат я получаю ошибку
[__NSCFDictionary length]: unrecognized selector sent to instance
так как я могу запросить имя и фотографию на Facebook и как обрабатывать входящие данные?
2 ответа
Решение
Я исправил это, следующий код работает нормально, чтобы получить имя 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];
}
}
Ответ, который дает вам сервер Facebook, - это один словарь, а не массив. Удалить две строки, касающиеся NSArray
и просто используйте:
if ([result objectForKey:@"first_name"]) {
[lblUser setText:[NSString stringWithFormat:@"Welcome %@!", [result objectForKey:@"first_name"]]];
NSLog(@"%@", lblUser);
// Show the publish button.
[btnPublish setHidden:NO];
}