前々回 と、さらにその前あたりで、Facebook周りを色々調べていましたが、Facebookの友達のプロフィール写真を取得するために色々頑張ってみました。
雑多な手順
プロジェクトに、
- Accounts.framework
- Social.framework
を追加して、
ACAccountStore *accountStore = [[ACAccountStore alloc]init]; ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook]; NSDictionary *options = @{ACFacebookAppIdKey: @"<FacebookアプリのID>", ACFacebookPermissionsKey: @[@"email", @"read_friendlists"] }; [accountStore requestAccessToAccountsWithType:accountType options:options completion:^(BOOL granted, NSError *error) { if (granted) { NSArray *accounts = [accountStore accountsWithAccountType:accountType]; ACAccount *anAccount = [accounts lastObject]; SLRequest *friendsListRequest = [SLRequest requestForServiceType:SLServiceTypeFacebook requestMethod:SLRequestMethodGET URL:[[NSURL alloc] initWithString:@"https://graph.facebook.com/me/friends"] parameters:nil]; friendsListRequest.account = anAccount; [friendsListRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) { NSError *jsonError = nil; NSDictionary *data = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingAllowFragments error:&jsonError]; for (NSDictionary *friend in data[@"data"]) { NSString *pictureURL = [[NSString alloc] initWithFormat:@"https://graph.facebook.com/%@/picture", friend[@"id"]]; NSLog(@"%@", pictureURL); // プロフィール写真のURL } }]; } else { NSLog(@"error: %@",[error description]); } }];
と、こんな感じで実装してみました。
APIすら叩いたことのない状態からスタートして、なにもかもがはじめての経験だったのですが、とりあえず友達のプロフィール写真のURLを取得することはできたので今回は良しとしておきましょう。きっともっとスマートな書きかたがあるのだと思います。
FacebookSDKを導入した際 にFacebookアプリをつくっておいたり、frameworkを追加する方法を調べたりしていたので、意外とすんなり進めることができました。
あまりわかってない部分もありますが、それはおいおい調べていきます。