0

我正在构建一个webview ios app使用 facebook 电子邮件。我用谷歌搜索了我的问题,所有这些答案都来自这个函数:

   NSArray *permissions = [NSArray arrayWithObjects:@"email", nil];
    return [FBSession openActiveSessionWithReadPermissions:permissions
                                              allowLoginUI:allowLoginUI
                                         completionHandler:^(FBSession *session, FBSessionState state, NSError *error) {
                                             [self sessionStateChanged:session state:state error:error];
                                         }];

但我想要的不是来自这个的 facebook 电子邮件地址。有什么办法吗?

如果我使用这个功能,我必须登录两次,这有点烦人。如果除此之外没有其他方法可以获取电子邮件地址,有没有办法避免该login过程两次?

4

2 回答 2

0

是的,您可以使用 iOS 6 内置的 Facebook 帐户(实际上不使用 FB SDK)来接收电子邮件。如果用户已经在 iOS 设置中登录 Facebook,那么只有第一次会提示他允许您的应用访问 FB 帐户。

首先,将其添加到您的界面:

#import <Social/Social.h>
#import <Accounts/Accounts.h>

@property (nonatomic, strong) ACAccountStore *accountStore;
@property (nonatomic, strong) ACAccount *facebookAccount;

然后使用它来实现:

- (void)getFacebookPermissions
{
    if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
        // we have facebook account in ios settings
        NSLog(@"Facebook account exists");

        ACAccountType *facebookTypeAccount = [self.accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];

        [self.accountStore requestAccessToAccountsWithType:facebookTypeAccount
                                                   options:@{ACFacebookAppIdKey: *YOUR_FACEBOOK_APP_ID_HERE!*, ACFacebookPermissionsKey: @[@"email"]}
                                                completion:^(BOOL granted, NSError *error) {
                                                    if (granted) {
                                                        self.facebookAccount = [[self.accountStore accountsWithAccountType:facebookTypeAccount] lastObject];

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

                                                        SLRequest *meRequest = [SLRequest requestForServiceType:SLServiceTypeFacebook
                                                                                                  requestMethod:SLRequestMethodGET
                                                                                                            URL:meURL
                                                                                                     parameters:nil];

                                                        meRequest.account = self.facebookAccount;

                                                        [meRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
                                                            if (error) {
                                                                NSLog(@"[ERROR] %@", [error localizedDescription]);
                                                                // or do some error handling
                                                            } else {
                                                                NSError *jsonError;
                                                                NSDictionary *responseJSON = [NSJSONSerialization JSONObjectWithData:responseData
                                                                                                                             options:NSJSONReadingAllowFragments
                                                                                                                               error:&jsonError];
                                                                if (jsonError)
                                                                {
                                                                    NSLog(@"[FB PARSING ERROR] %@", [error localizedDescription]);
                                                                } else {
                                                                    // here it is!
                                                                    NSString *email = [responseJSON objectForKey:@"email"];
                                                                }
                                                            }

                                                        }];


                                                    } else {
                                                        if (error) {
                                                            NSLog(@"[ERROR] %@", [error localizedDescription]);

                                                        }
                                                        else
                                                        {
                                                            NSLog(@"Access to Facebook was not granted. Please go to the device settings and allow access for *YOUR_APP_NAME*");
                                                        }
                                                    }
                                                }];
    } else {
        NSLog(@"There was an error retrieving your Facebook account, make sure you have an account setup in Settings and that access is granted for *YOUR_APP_NAME*");
    }

}

不要忘记创建 AccountStore 的实例并弱链接 Social 和 Accounts 框架。

PS 这是您的 Facebook 应用程序的设置(您可以在顶部看到您的应用程序 ID): 在此处输入图像描述

希望这可以帮助!

于 2013-03-21T09:49:58.050 回答
0

您可以+requestForMe为此使用:

-(void)getUserEmail
{
    [[FBRequest requestForMe] startWithCompletionHandler:^(FBRequestConnection *connection, NSDictionary <FBGraphUser> *user, NSError *error){
            if (!error) {
                NSString *email = [user objectForKey:@"email"];
                NSLog(@"email : %@", email);
            }
        }];
}
于 2013-03-21T10:55:26.793 回答