Just My Life & My Work

怎麼在APP裡使用臉書獲取使用者資料 (Using Facebook to Obtain User Information)呢?過去要去下載Facebook SDK,現在有更便捷的做法,就是iOS SDK有把Facebook整合進去,只要幾個步驟即可讓使用者授權臉書的個人資料

facebook

首先在專案加入框架:Accounts.framework

接著在.h檔中寫:

#import <Accounts/Accounts.h>
@property (nonatomic, strong) ACAccountStore *accountStore;
property (nonatomic, strong) ACAccount *facebookAccount;

最後再.m檔中寫:

/**
 Theme: Using Facebook to Obtain User Information
 IDE: Xcode 5
 Language: Objective C
 Date: 102/09/09
 Author: HappyMan
 Blog: https://cg2010studio.wordpress.com/
 */
- (IBAction)facebookFetchData:(UIButton *)button
{
    if(self.accountStore == nil){
        self.accountStore = [[ACAccountStore alloc] init];
    }
    ACAccountType *facebookAccountType = [self.accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
    NSString *facebookAppIDStr = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"FacebookAppID"];

    NSDictionary *options = @{
                              ACFacebookAppIdKey : facebookAppIDStr,
                              ACFacebookPermissionsKey : @[@"email", @"public_profile"],
                              ACFacebookAudienceKey : ACFacebookAudienceEveryone}; // Needed only when write permissions are requested

    [self.accountStore requestAccessToAccountsWithType:facebookAccountType options:options
                                            completion:^(BOOL granted, NSError *error) {
                                                if (granted)
                                                {
                                                    NSArray *accounts = [self.accountStore
                                                                         accountsWithAccountType:facebookAccountType];
                                                    self.facebookAccount = [accounts lastObject];
                                                    NSLog(@"facebookAccount oauthToken: %@", self.facebookAccount.credential.oauthToken);
                                                } else {
                                                    NSLog(@"error: %@", error);
                                                    // Fail gracefully...
                                                }
                                            }];
}

獲取的資料:

<__NSArrayI 0x1d51e940>(
type:com.apple.facebook
identifier:
accountDescription: Facebook
username:
objectID: x-coredata://
enabledDataclasses: {(
“com.apple.Dataclass.Calendars",
“com.apple.Dataclass.Contacts"
)}
enableAndSyncableDataclasses: {(
)}
properties: {
fullname = “HappyMan";
uid = 1234567890;
}
parentAccount: (null)
owningBundleID:(null)
)

要記得去臉書開發者後台設定原生iOS程式的Bundle ID和開啓iOS連結喔~

facebook 後台 app 資料

這是臉書開發者後台,有App ID和App Secret。

程式碼中ACFacebookAppIdKey的值要記得改為自己的App ID喔!

Xcode Bundle ID

在Xcode中的Bundle ID。

facebook 後台

記得開啓Facebook登入。

如果沒有設定相對應的Bundle ID,授權時就會出現error code:

Error Domain=com.apple.accounts Code=7 “The Facebook server could not fulfill this access request: remote_app_id does not match stored id " UserInfo=0x165971f0 {NSLocalizedDescription=The Facebook server could not fulfill this access request: remote_app_id does not match stored id }

順利使用臉書的前提是要在系統的設定(Setting)登入Facebook,不必下載Facebook APP,接著授權給我們的APP,若想更動授權狀態關閉或打開,要去設定(Setting)那兒去轉開關。

setting Facebook & Twitter

更新160301:

FacebookAppID為Info.plist上設定的key。

執行後順利登入顯示訊息:
facebookAccount oauthToken: CAAOSYCN05JoBAEpCD9CMnM0ekkTl3umX2UmYK50JyCK5RqR30VVnAPJB5i63ZCSigF3Xplq7yP3zvZBVOmYIVcgZCZAETVjnaDIIs9ZCrl2p41O29klTMZBZCujvCxjPUK7bsujs7ZCGKkbVBJYs6iNCSWisr0Bh6lVARvF8VTpDzMOFnpZB3meZBRMeccKTDxoCbuxlZBozyo7tBHfPQkHKlEJn2odV7XUqJpfboMfkgvUvwZDZDX

參考:使用 Social Framework 對社群網站發佈訊息Facebook Integration Error ( Accounts.framework) in iOS6

Comments on: "[iOS] 使用臉書獲取使用者資料 (Using Facebook to Obtain User Information)" (2)

  1. […] 參考:【iOS/Facebook】在 iOS 上加入 Facebook 登入功能、Facebook SDK 取得帳號資料、使用臉書獲取使用者資料 (Using Facebook to Obtain User Information)。 […]

  2. […] 先前有使用iOS SDK直接取得臉書帳號授權,可惜只能拿到ID與name,現在想取得使用者的email,只好使用Facebook SDK取得帳號資料! […]

隨意留個言吧:)~

在下方填入你的資料或按右方圖示以社群網站登入:

WordPress.com 標誌

您的留言將使用 WordPress.com 帳號。 登出 /  變更 )

Facebook照片

您的留言將使用 Facebook 帳號。 登出 /  變更 )

連結到 %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

標籤雲

%d 位部落客按了讚: