Querying Facebook user data through new iOS6 social framework

后端 未结 6 2037
轮回少年
轮回少年 2020-12-28 10:41

I am trying to query information about a user using iOS 6\'s new Facebook integration API. This is the code I\'m using, which is basically identical to what they demoed at W

6条回答
  •  南方客
    南方客 (楼主)
    2020-12-28 11:05

    The answer is simple

    in viewDidLoad() use:

    accountStore= [[ACAccountStore alloc]init];
    facebookAccountType= [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
    
    NSDictionary *options= @{
    ACFacebookAudienceKey: ACFacebookAudienceEveryone,
    ACFacebookAppIdKey: @"",
    ACFacebookPermissionsKey: @[@"public_profile"]
    
                              };    
    
    [accountStore requestAccessToAccountsWithType:facebookAccountType options:options completion:^(BOOL granted, NSError *error) {
    
        if (granted) {
            NSLog(@"Permission has been granted to the app");
            NSArray *accounts= [accountStore accountsWithAccountType:facebookAccountType];
            facebookAccount= [accounts firstObject];
            [self performSelectorOnMainThread:@selector(facebookProfile) withObject:nil waitUntilDone:NO];
    
        } else {
            NSLog(@"Permission denied to the app");
        }
    }];
    

    And the function-(void)facebookProfile

    NSURL *url = [NSURL URLWithString:@"https://graph.facebook.com/me"];
    

    Notice the params you need are added as dictionary Refer below for complete list https://developers.facebook.com/docs/graph-api/reference/user

    NSDictionary *param=[NSDictionary dictionaryWithObjectsAndKeys:@"picture,id,name",@"fields", nil];
    
    SLRequest *profileInfoRequest= [SLRequest requestForServiceType:SLServiceTypeFacebook requestMethod:SLRequestMethodGET URL:url parameters:param];
    profileInfoRequest.account= facebookAccount;
    
    
    [profileInfoRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
        NSLog(@"Facebook status code is : %ld", (long)[urlResponse statusCode]);
    
        if ([urlResponse statusCode]==200) {
    
            NSDictionary *dictionaryData= [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves error:&error];
    
    
    
        } else {
    
        }
    }];
    
    
    }
    

提交回复
热议问题