I'm building a webview ios app
which uses facebook email. I googled for my problem and all those answers are something from this function:
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];
}];
But I want facebook email address not from this. Is there any way to do it?
If I'm using this function, I have to login twice which is kind of annoying. If there isn't a way to get email address other than this, is there a way to avoid the login
process twice?
Yes, you can get email using iOS 6 built in Facebook account (and actually without using FB SDK). If user already logged in Facebook in iOS settings, then he first time only will be prompted to allow your App access to FB account.
At first, add this to your Interface:
#import <Social/Social.h>
#import <Accounts/Accounts.h>
@property (nonatomic, strong) ACAccountStore *accountStore;
@property (nonatomic, strong) ACAccount *facebookAccount;
Then use this to Implementation:
- (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*");
}
}
Don't forget to make an instance of AccountStore and to weak link Social and Accounts frameworks.
PS Here is the settings for your Facebook App (you can see your App ID at the top):
Hope this helps!
You can use +requestForMe
for that :
-(void)getUserEmail
{
[[FBRequest requestForMe] startWithCompletionHandler:^(FBRequestConnection *connection, NSDictionary <FBGraphUser> *user, NSError *error){
if (!error) {
NSString *email = [user objectForKey:@"email"];
NSLog(@"email : %@", email);
}
}];
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.