是的,您可以使用 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):
希望这可以帮助!