如何使用Facebook获取用户的电子邮件? [英] How to fetch user's email using Facebook?
问题描述
loginViewFetchedUserInfo $获取用户(Facebook)的 first_name
, last_name
c $ c>方法 FBLoginView
,但我想知道如何获取电子邮件,生日等。
以下是我的代码:
in viewDidLoad
FBLoginView * loginview = [[FBLoginView alloc] init];
loginview.readPermissions = @ [@email];
loginview.frame = CGRectOffset(loginview.frame,5,5);
loginview.delegate = self;
[self.view addSubview:loginview];
[loginview sizeToFit];
此后如何以及在哪里可以获取用户的电子邮件地址?
我已经经历了这么多帖子,但对我没有任何意义,所以请帮忙。
谢谢!
解决方案新代码facebook SDK ver 4.0及以上
看到这个链接
以下
//使用facebook SDK 3.8
在AppDelegate.m中添加以下方法
$ (UIApplication *)应用程序openURL(NSURL *)url sourceApplication:(NSString *)sourceApplication注释:(id)注释
{
return [FBAppCall handleOpenURL:url sourceApplication:sourceApplication fallbackHandler:^(FBAppCall * call)
{
NSLog(@Facebook handler);
}
];
}
- (void)applicationDidBecomeActive :( UIApplication *)应用程序
{
[FBAppEvents activateApp];
[FBAppCall handleDidBecomeActive];
//在应用程序处于非活动状态时,重新启动任何已暂停(或尚未启动)的任务。如果应用程序以前是在后台,请选择刷新用户界面。
}
- (void)applicationWillTerminate :( UIApplication *)应用程序
{
[FBSession.activeSession close];
//当应用程序即将终止时调用。酌情保存数据。另请参见applicationDidEnterBackground :.
}
查看您的viewcontroler中的以下代码.h
#import< UIKit / UIKit.h>
#import< FacebookSDK / FacebookSDK.h>
#import< CoreLocation / CoreLocation.h>
@interface ViewController:UIViewController< FBLoginViewDelegate>
@property(强,非原子)IBOutlet UILabel * lblUserName;
@property(strong,nonatomic)IBOutlet UITextField * txtEmailId;
@property(强,非原子)IBOutlet UIButton * lblCreate;
@property(强,非原子)IBOutlet FBProfilePictureView * profilePic;
@property(strong,nonatomic)id< FBGraphUser>与loggedInUser;
- (IBAction)butCreate :((id)sender;
- (void)showAlert:(NSString *)消息
结果:(id)结果
错误:(NSError *)错误;
@end
//将以下代码应用于视图控制器。 m
- (void)viewDidLoad
{
[super viewDidLoad];
FBLoginView * loginview = [[FBLoginView alloc] initWithReadPermissions:@ [@email,@user_likes]];
loginview.frame = CGRectMake(60,50,200,50);
loginview.delegate = self;
[loginview sizeToFit];
[self.view addSubview:loginview];
}
- (void)loginViewShowingLoggedInUser:(FBLoginView *)loginView
{
self.lblCreate.enabled = YES;
self.txtEmailId.enabled = YES;
self.lblUserName.enabled = YES;
}
- (void)loginViewFetchedUserInfo:(FBLoginView *)loginView用户:(id< FBGraphUser>)用户
{
self.lblUserName.text = [NSString stringWithFormat:@%@,user.name];
self.txtEmailId.text = [user objectForKey:@email];
//self.profilePic.profileID=user.id;
self.loggedInUser = user;
}
- (void)loginViewShowingLoggedOutUser:(FBLoginView *)loginView
{
self.txtEmailId.text = nil;
self.lblUserName.text = nil;
self.loggedInUser = nil;
self.lblCreate.enabled = NO;
}
- (void)loginView:(FBLoginView *)loginView handleError:(NSError *)错误{
NSLog(@显示错误==%@,错误);
}
Swift 1.2&
创建一个字典:
class ViewController :UIViewController {
var dict:NSDictionary!
}
获取数据:
if((FBSDKAccessToken.currentAccessToken())!= nil){
FBSDKGraphRequest(graphPath:me,参数:[fields:id,name, first_name,last_name,picture.type(large),email])。startWithCompletionHandler({(connection,result,error))> Void in
if(error == nil){
self.dict = result as NSDictionary
println(self.dict)
NSLog(self.dict.objectForKey(picture)?. objectForKey(data)?。objectForKey(url)as String)
}
})
}
输出应该是: p>
{
email =karthik.saral@gmail.com;
first_name= Karthi;
id = 924483474253864;
last_name= keyan;
name =karthi keyan;
picture = {
data = {
is_silhouette= 0;
url =XXXXXXX;
};
};
}
I am new to iOS. I am able to fetch first_name
,last_name
of user(Facebook) using loginViewFetchedUserInfo
method of FBLoginView
but I want to know how to fetch the email,birthday and etc.
Here is my code:
in viewDidLoad
FBLoginView *loginview = [[FBLoginView alloc] init];
loginview.readPermissions=@[@"email"];
loginview.frame = CGRectOffset(loginview.frame, 5, 5);
loginview.delegate = self;
[self.view addSubview:loginview];
[loginview sizeToFit];
After this how and where can I fetch the email address of the user?
I have gone through so many posts but they didn't make any sense to me, so please help. Thanks!
for new code facebook SDK ver 4.0 and above
see this link
below
// use facebook SDK 3.8
add the following methods in AppDelegate.m
-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication: (NSString *)sourceApplication annotation:(id)annotation
{
return [FBAppCall handleOpenURL:url sourceApplication:sourceApplication fallbackHandler:^(FBAppCall *call)
{
NSLog(@"Facebook handler");
}
];
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
[FBAppEvents activateApp];
[FBAppCall handleDidBecomeActive];
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
- (void)applicationWillTerminate:(UIApplication *)application
{
[FBSession.activeSession close];
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
se the follwing code in your viewcontroler .h
#import <UIKit/UIKit.h>
#import <FacebookSDK/FacebookSDK.h>
#import <CoreLocation/CoreLocation.h>
@interface ViewController : UIViewController<FBLoginViewDelegate>
@property (strong, nonatomic) IBOutlet UILabel *lblUserName;
@property (strong, nonatomic) IBOutlet UITextField *txtEmailId;
@property (strong, nonatomic) IBOutlet UIButton *lblCreate;
@property (strong, nonatomic) IBOutlet FBProfilePictureView *profilePic;
@property (strong, nonatomic) id<FBGraphUser> loggedInUser;
- (IBAction)butCreate:(id)sender;
- (void)showAlert:(NSString *)message
result:(id)result
error:(NSError *)error;
@end
// apply the below code to your view controller.m
- (void)viewDidLoad
{
[super viewDidLoad];
FBLoginView *loginview=[[FBLoginView alloc]initWithReadPermissions:@[@"email",@"user_likes"]];
loginview.frame=CGRectMake(60, 50, 200, 50);
loginview.delegate=self;
[loginview sizeToFit];
[self.view addSubview:loginview];
}
-(void)loginViewShowingLoggedInUser:(FBLoginView *)loginView
{
self.lblCreate.enabled=YES;
self.txtEmailId.enabled=YES;
self.lblUserName.enabled=YES;
}
-(void)loginViewFetchedUserInfo:(FBLoginView *)loginView user:(id<FBGraphUser>)user
{
self.lblUserName.text=[NSString stringWithFormat:@"%@",user.name];
self.txtEmailId.text=[user objectForKey:@"email"];
//self.profilePic.profileID=user.id;
self.loggedInUser=user;
}
-(void)loginViewShowingLoggedOutUser:(FBLoginView *)loginView
{
self.txtEmailId.text=nil;
self.lblUserName.text=nil;
self.loggedInUser=nil;
self.lblCreate.enabled=NO;
}
-(void)loginView:(FBLoginView *)loginView handleError:(NSError *)error{
NSLog(@"Show the Error ==%@",error);
}
Swift 1.2 & above
Create a dictionary :
class ViewController: UIViewController {
var dict : NSDictionary!
}
Fetching the data :
if((FBSDKAccessToken.currentAccessToken()) != nil){
FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, picture.type(large), email"]).startWithCompletionHandler({ (connection, result, error) -> Void in
if (error == nil){
self.dict = result as NSDictionary
println(self.dict)
NSLog(self.dict.objectForKey("picture")?.objectForKey("data")?.objectForKey("url") as String)
}
})
}
Output should be :
{
email = "karthik.saral@gmail.com";
"first_name" = Karthi;
id = 924483474253864;
"last_name" = keyan;
name = "karthi keyan";
picture = {
data = {
"is_silhouette" = 0;
url = "XXXXXXX";
};
};
}
这篇关于如何使用Facebook获取用户的电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!