如何在Facebook中自定义登录按钮?我不想使用Facebook默认登录按钮。
答案 0 :(得分:15)
对于SDK 4.0:
你应该添加一个按钮,并在按钮操作中使用以下代码:
- (IBAction)loginButtonClicked:(id)sender {
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
[login logInWithReadPermissions:@[@"email"] handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
if (error) {
// Process error
NSLog(@"error %@",error);
} else if (result.isCancelled) {
// Handle cancellations
NSLog(@"Cancelled");
} else {
if ([result.grantedPermissions containsObject:@"email"]) {
// Do work
NSLog(@"%@",result);
NSLog(@"Correct");
}
}
}];
}
已更新:要接收用户信息
- (IBAction)loginButtonClicked:(id)sender {
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
[login logInWithReadPermissions:@[@"email"] handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
if (error) {
// Process error
NSLog(@"error %@",error);
} else if (result.isCancelled) {
// Handle cancellations
NSLog(@"Cancelled");
} else {
if ([result.grantedPermissions containsObject:@"email"]) {
// Do work
[self fetchUserInfo];
}
}
}];
}
-(void)fetchUserInfo {
if ([FBSDKAccessToken currentAccessToken]) {
NSLog(@"Token is available");
[[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:nil]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error) {
NSLog(@"Fetched User Information:%@", result);
}
else {
NSLog(@"Error %@",error);
}
}];
} else {
NSLog(@"User is not Logged in");
}
}
答案 1 :(得分:1)
您应该添加UIButton
并添加如下点击方法:
- (IBAction)fbLogin:(id)sender {
[FBSession.activeSession closeAndClearTokenInformation];
[FBSession openActiveSessionWithReadPermissions:@[@"public_profile"]
allowLoginUI:YES
completionHandler:^(FBSession *fbSession, FBSessionState state, NSError *error) {
[self sessionStateChanged:fbSession state:state error:error];
}];
}
答案 2 :(得分:0)
FBLoginView *fbLoginView = [[FBLoginView alloc] initWithPermissions:[NSArray arrayWithObject:@"publish_actions"]];
fbLoginView.frame = CGRectMake(0, 200, 271, 37);
for (id obj in fbLoginView.subviews)
{
if ([obj isKindOfClass:[UIButton class]])
{
UIButton * loginButton = obj;
UIImage *loginImage = [UIImage imageNamed:@"YourImg.png"];
[loginButton setBackgroundImage:loginImage forState:UIControlStateNormal];
[loginButton setBackgroundImage:nil forState:UIControlStateSelected];
[loginButton setBackgroundImage:nil forState:UIControlStateHighlighted];
[loginButton sizeToFit];
}
}
答案 3 :(得分:0)
我刚刚在iOS 9中结束。将此代码放在按钮操作方法中。
[FBSession openActiveSessionWithReadPermissions:@[@"public_profile",@"email",@"user_friends"]
allowLoginUI:YES
completionHandler:
^(FBSession *session, FBSessionState state, NSError *error) {
// The session is open. Get the user information and update the UI.
[FBRequestConnection startWithGraphPath:@"me"
parameters:@{@"fields": @"first_name,last_name, email"}
HTTPMethod:@"GET"
completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (!error) {
// Set the use full name.
NSLog(@"FBresponse: =>%@",result);
}
else{
NSLog(@"%@", [error localizedDescription]);
}
}];
}];