Sri*_*ddy 1 iphone facebook ios
我是iOS新手.我能够获取first_name,last_name使用用户(脸谱)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];
Run Code Online (Sandbox Code Playgroud)
在此之后我如何以及在哪里可以获取用户的电子邮件地址?
我经历了这么多帖子,但对我没有任何意义,所以请帮忙.谢谢!
Anb*_*hik 11
对于新代码facebook SDK ver 4.0及以上版本
看到这个链接
下面
// use facebook SDK 3.8
Run Code Online (Sandbox Code Playgroud)
在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:.
}
Run Code Online (Sandbox Code Playgroud)
请查看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
Run Code Online (Sandbox Code Playgroud)
//将以下代码应用于您的视图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);
}
Run Code Online (Sandbox Code Playgroud)
Swift 1.2及以上版本
创建字典:
class ViewController: UIViewController {
var dict : NSDictionary!
}
Run Code Online (Sandbox Code Playgroud)
获取数据:
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)
}
})
}
Run Code Online (Sandbox Code Playgroud)
输出应该是:
{
email = "karthik.saral@gmail.com";
"first_name" = Karthi;
id = 924483474253864;
"last_name" = keyan;
name = "karthi keyan";
picture = {
data = {
"is_silhouette" = 0;
url = "XXXXXXX";
};
};
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3876 次 |
| 最近记录: |