簡體   English   中英

如何使用Facebook獲取用戶的電子郵件?

[英]How to fetch user's email using Facebook?

我是iOS新手。 我能夠獲取first_namelast_name的用戶(Facebook)的使用loginViewFetchedUserInfo的方法FBLoginView但我想知道如何獲取電子郵件,生日等。

這是我的代碼:

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 4.0及更高版本

看到這個鏈接

下面

 //  use facebook SDK 3.8 

在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:.
}

在您的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

//將以下代碼應用於您的視圖控制器

- (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及更高版本

創建字典:

class ViewController: UIViewController {
    var dict : NSDictionary!
}

提取數據:

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)
        }
    })
}

輸出應為:

{
    email = "karthik.saral@gmail.com";
    "first_name" = Karthi;
    id = 924483474253864;
    "last_name" = keyan;
    name = "karthi keyan";
    picture =     {
        data =         {
            "is_silhouette" = 0;
            url = "XXXXXXX";
        };
    };
}

嘗試這個..

if(FBSession.activeSession.isOpen) {

       [FBRequestConnection startForMeWithCompletionHandler:^(FBRequestConnection *connection,
                                                           id<FBGraphUser> user,
                                                           NSError *error) {
        if (!error) {
                       NSString *email = [user objectForKey:@"email"];
                    }
   }];
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM