我无法从iOS中的Facebook获取用户数据
我是新来的iPhone编程。使用下面的代码我login到Facebook和获取数据。但这里的问题是,如果点击LoginwithfacebookbuttonFacebook对话框打开。那么我inputFacebook的用户名和密码,如果再次点击相同button,然后我得到用户数据。但我不想点击双倍的时间。而且,一旦我loginFacebook.inside的应用程序,我想获取当时的用户数据,我不会再login一次直到我点击注销。请给我最好的主意。
-(void)LoginWithFacebookk:(id)sender { if (!FBSession.activeSession.isOpen) { FBSession *session = [[FBSession alloc] init]; // Set the active session [FBSession setActiveSession:session]; // Open the session [session openWithBehavior:FBSessionLoginBehaviorForcingWebView completionHandler:^(FBSession *session, FBSessionState status, NSError *error) { }]; } else { NSLog(@"open response"); FBRequest* friendsRequest = [FBRequest requestForMe]; [friendsRequest startWithCompletionHandler: ^(FBRequestConnection *connection,NSDictionary <FBGraphUser> *user,NSError *error) { NSString *emailiddd=[NSString stringWithFormat:@"%@",[user objectForKey:@"email"]]; NSLog(@"%@",emailiddd); NSString *post = [NSString stringWithFormat:@"email=%@",emailiddd]; NSLog(@"%@",post); }]; } } }
如果您在FacebookSDK
中查看Facebook提供的示例代码,则有一个示例SessionLoginSample,请检查工作方式:
你必须像这样configuration你的应用程序:
在本机iOS应用程序中,您必须设置您使用的应用程序包ID,例如您使用的项目( com.compame.projecname )
为了实施你的项目在这里我把解释。
在.h文件中用IBOutlet
创build一个button,并将这个button连接到xib中,就像我们通常那样
#import <FacebookSDK/FacebookSDK.h> @interface LoginViewController : UIViewController<FBLoginViewDelegate> { IBOutlet UIButton *FB_login; // just connect IBoutlate no need to give IBAction FBLoginView *loginview; } @property (strong, nonatomic) id<FBGraphUser> loggedInUser; @property (strong, nonatomic) IBOutlet FBProfilePictureView *profilePic; //this is for show profile pic or logged in user
在.m文件中 @implementation LoginViewController @synthesize profilePic = _profilePic;
- (void)viewDidLoad { loginview = [[FBLoginView alloc] init]; loginview = [[FBLoginView alloc] initWithPublishPermissions:[NSArray arrayWithObjects:@"publish_actions",@"email",nil] defaultAudience:FBSessionDefaultAudienceFriends]; loginview.frame =FB_login.frame;//CGRectMake(90,149, 280, 55); for (id obj in loginview.subviews) { if ([obj isKindOfClass:[UIButton class]]) { UIButton * loginButton = obj; UIImage *loginImage = [UIImage imageNamed:@"btn_fb_login.png"]; [loginButton setBackgroundImage:loginImage forState:UIControlStateNormal]; [loginButton setBackgroundImage:nil forState:UIControlStateSelected]; [loginButton setBackgroundImage:nil forState:UIControlStateHighlighted]; [loginButton sizeToFit]; } if ([obj isKindOfClass:[UILabel class]]) { UILabel * loginLabel = obj; loginLabel.text =@""; //@"Log in to facebook"; loginLabel.textAlignment = NSTextAlignmentCenter; loginLabel.frame =CGRectMake(123,149, 280, 55);// CGRectMake(0, 0, 271, 37); } } loginview.delegate = self; [self.view addSubview:loginview]; [super viewDidLoad]; } -(void)LogOut:(NSNotification *)notif { [self loginViewShowingLoggedOutUser:loginview]; NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage]; for (NSHTTPCookie *each in cookieStorage.cookies) { // put a check here to clear cookie url which starts with twitter and then delete it [cookieStorage deleteCookie:each]; } } #pragma mark------ FaceBook Sign In method------------ - (void)loginViewShowingLoggedInUser:(FBLoginView *)loginView { NSLog(@"Logged In"); } - (void)loginViewFetchedUserInfo:(FBLoginView *)loginView user:(id<FBGraphUser>)user { NSLog(@"%@",user); self.profilePic.profileID = user.id; } - (void)loginViewShowingLoggedOutUser:(FBLoginView *)loginView { // Called after logout NSLog(@"Logged out"); self.profilePic.profileID = nil; [FBSession.activeSession closeAndClearTokenInformation]; } - (void)loginView:(FBLoginView *)loginView handleError:(NSError *)error { // see https://developers.facebook.com/docs/reference/api/errors/ for general guidance on error handling for Facebook API // our policy here is to let the login view handle errors, but to log the results NSLog(@"FBLoginView encountered an error=%@", error); }
注意: –如果您正尝试使用FBProfilePictureView
设置login用户的图像,请不要忘记添加[FBProfilePictureView class];
在应用程序像贝娄的代表
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [FBProfilePictureView class]; // Override point for customization after application launch. return YES; }
这里是一个示例代码 示例代码Facebooklogin与图像
如果你愿意,你也可以用Parse来做:
- (IBAction)facebookRegistrationButtonPressed { // The permissions requested from the user NSArray *permissionsArray = @[@"email", @"user_about_me", @"publish_actions", @"status_update", @"manage_pages", @"publish_stream"]; // Login PFUser using Facebook [PFFacebookUtils logInWithPermissions:permissionsArray block:^(PFUser *user, NSError *error) { if (!user) { if (!error) { NSLog(@"Uh oh. The user cancelled the Facebook login."); } else { NSLog(@"Uh oh. An error occurred: %@", error); } } else if (user.isNew) { NSLog(@"User with facebook signed up and logged in!"); [self processFacebookRequest]; } else { NSLog(@"User with facebook logged in!"); [self processFacebookRequest]; } }]; } - (void)processFacebookRequest { FBRequest *request = [FBRequest requestForMe]; // Send request to Facebook [request startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) { if (!error) { // result is a dictionary with the user's Facebook data NSDictionary *userData = (NSDictionary *)result; NSString * email = [result objectForKey:@"email"]; NSString *facebookID = userData[@"id"]; NSLog(@"facebookID = %@", facebookID); } else { NSLog(@"error : %@", error.localizedDescription); } }]; }
- 通过Facebook Graph API获取个人资料图片返回“不支持的URL”
- Facebook iOS SDK 3.x提要对话框消失了吗?
- 非常奇怪NSInvalidArgumentException与addSubview FBLoginView
- 是否有可能仅从本地移动应用程序使用来自Facebook的Scores&Achievements API?
- 使用自定义URLscheme的iOS无缝Facebooklogin体验
- iOS Facebook框架login过程
- 照片对我的朋友不可见,但在我的时间表上
- 在应用程序相册中使用Facebook SDK 3.1 for iOS发布带有标题的图片
- iPhone中的fbconnect API响应