Custom Facebook Login button iOS
New URL of documentation about custom buttons:
https://developers.facebook.com/docs/facebook-login/ios/advanced
Or if you just want to know what to do when auth button tapped do this in "Button tapped" method (don't forget to link that method with your button):
#import <FBSDKLoginKit/FBSDKLoginKit.h>
#import <FBSDKCoreKit/FBSDKCoreKit.h>
Then
- (IBAction)facebookAuthButtonTapped:(id)sender {
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
[login
logInWithReadPermissions: @[@"public_profile"]
fromViewController:self
handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
if (error) {
NSLog(@"Process error");
} else if (result.isCancelled) {
NSLog(@"Cancelled");
} else {
NSLog(@"Logged in");
}
}];
}
SwiftUI solution
import SwiftUI
import FBSDKLoginKit
import Firebase
struct FaceBookLoginButton: UIViewRepresentable {
func makeCoordinator() -> FBSignInCoordinator {
return FBSignInCoordinator()
}
func makeUIView(context: UIViewRepresentableContext<FaceBookLoginButton>) -> FBLoginButton {
let view = FBLoginButton()
view.permissions = ["email"]
view.delegate = context.coordinator
// normal
view.setTitleColor(.clear, for: .normal)
view.setImage(nil, for: .normal)
view.setBackgroundImage(nil, for: .normal)
// tapped
view.setTitleColor(.clear, for: .highlighted)
view.setImage(nil, for: .highlighted)
view.setBackgroundImage(nil, for: .highlighted)
return view
}
func updateUIView(_ uiView: FBLoginButton, context: UIViewRepresentableContext<FaceBookLoginButton>) {}
}
Updated for Swift 3
@IBAction func fblogin(_ sender: Any) {
let loginManager = LoginManager()
UIApplication.shared.statusBarStyle = .default // remove this line if not required
loginManager.logIn([ .publicProfile,.email ], viewController: self) { loginResult in
print(loginResult)
//use picture.type(large) for large size profile picture
let request = GraphRequest(graphPath: "me", parameters: ["fields":"email,name,gender,picture"], accessToken: AccessToken.current, httpMethod: .GET, apiVersion: FacebookCore.GraphAPIVersion.defaultVersion)
request.start { (response, result) in
switch result {
case .success(let value):
print(value.dictionaryValue)
case .failed(let error):
print(error)
}
}
}
}
For Objective-C
You can call this method on UIButton
click event
-(void)fblogin{
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
if ([UIApplication.sharedApplication canOpenURL:[NSURL URLWithString:@"fb://"]])
{
login.loginBehavior = FBSDKLoginBehaviorSystemAccount;
}
[login logInWithReadPermissions:@[@"public_profile", @"email"] handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
if (error)
{
NSLog(@"Unexpected login error: %@", error);
NSString *alertMessage = error.userInfo[FBSDKErrorLocalizedDescriptionKey] ?: @"There was a problem logging in. Please try again later.";
NSString *alertTitle = error.userInfo[FBSDKErrorLocalizedTitleKey] ?: @"Oops";
[[[UIAlertView alloc] initWithTitle:alertTitle
message:alertMessage
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil] show];
}
else
{
if(result.token) // This means if There is current access token.
{
[[[FBSDKGraphRequest alloc] initWithGraphPath:@"me"
parameters:@{@"fields": @"picture, name, email"}]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id userinfo, NSError *error) {
if (!error) {
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^(void) {
dispatch_async(dispatch_get_main_queue(), ^{
// you are authorised and can access user data from user info object
});
});
}
else{
NSLog(@"%@", [error localizedDescription]);
}
}];
}
NSLog(@"Login Cancel");
}
}];
}
Make you own custom button in the storyboard. Hook up the action to myButtonPressed
.
- (void)viewDidLoad {
[super viewDidLoad];
self.loginButton = [[FBSDKLoginButton alloc] init];
self.loginButton.hidden = YES;
}
- (void)myButtonPressed {
[self.loginButton sendActionsForControlEvents: UIControlEventTouchUpInside];
}