2013-10-24 124 views
1

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

這裏是我的代碼:

viewDidLoad

FBLoginView *loginview = [[FBLoginView alloc] init]; 
[email protected][@"email"]; 
loginview.frame = CGRectOffset(loginview.frame, 5, 5); 
loginview.delegate = self; 

[self.view addSubview:loginview]; 

[loginview sizeToFit]; 

該如何後,我從哪裏可以獲取用戶的電子郵件地址是什麼?

我經歷了那麼多帖子走了,但他們沒有任何意義,我,所以請大家幫忙。 謝謝!

回答

11

對新代碼的Facebook SDK版本4.0及以上

看到這個link

下面

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

本身的follwing代碼在你的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 

//應用下面的代碼到視圖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); 
} 

夫特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 = "[email protected]"; 
    "first_name" = Karthi; 
    id = 924483474253864; 
    "last_name" = keyan; 
    name = "karthi keyan"; 
    picture =  { 
     data =   { 
      "is_silhouette" = 0; 
      url = "XXXXXXX"; 
     }; 
    }; 
} 
+0

由於其工作的罰款... –

+0

電子郵件返回null,我請大家幫忙。 –

+0

@PradumnaPatil - 這是舊的代碼,SDK是你使用,你可以展示你的代碼 –

2

試試這個..

if(FBSession.activeSession.isOpen) { 

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