Configure .Plist File
In .Plist File
Add FacebookAppID
Eg. open Info.plist file
Add new Row & Add like this.
FacebookAppID : string : 1376543139268660
This FacebookAppID Number
you can get from
Registering your Application From Facebook API.
For More Info Click here
Follow step given in Facebook link & you can get Facebook App ID & Set it in .PList File.
Download Facebook SDK & Import it.
in ViewController .h file
Add below Code.
#import <FacebookSDK/FacebookSDK.h>
@property (weak, nonatomic) IBOutlet UIButton *btnFacebook;
- (IBAction)btnFacebook:(id)sender;
@property (weak, nonatomic) IBOutlet UIImageView *profilePictureView;
// In your view header file:
@property (weak, nonatomic) IBOutlet FBLoginView *loginView;
@property (retain, nonatomic) IBOutlet UIWebView *tempWebView;
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *activityIndicator;
in ViewController .m file
Set Delegate in ViewController Like this.
@interface LoginViewController ()<FBLoginViewDelegate>
@synthesize btnFacebook,profilePictureView,tempWebView;
- (IBAction)btnFacebook:(id)sender {
NSLog(@"BTN_Facebook");
//set Read Permissions to Facebook SDK
FBLoginView *loginView =
[[FBLoginView alloc] initWithReadPermissions:
@[@"public_profile", @"email", @"user_friends"]];
loginView.delegate = self;
//set frame
loginView.frame = CGRectMake(30, 226, 260, 53);
for (id obj in self.loginView.subviews)
{
if ([obj isKindOfClass:[UIButton class]])
{
//Create Custom Login Button USing ByDefault Image of Facebook given in SDK
UIButton * loginButton = obj;
UIImage *loginImage = [UIImage imageNamed:@"signfacebook.png"];
[loginButton setBackgroundImage:loginImage forState:UIControlStateNormal];
[loginButton setBackgroundImage:nil forState:UIControlStateSelected];
[loginButton setBackgroundImage:nil forState:UIControlStateHighlighted];
[loginButton setTitle:nil forState:UIControlStateSelected];
[loginButton setTitle:nil forState:UIControlStateHighlighted];
[loginButton sizeToFit];
}
if ([obj isKindOfClass:[UILabel class]])
{
UILabel * loginLabel = obj;
loginLabel.text = @"";
loginLabel.textAlignment = NSTextAlignmentCenter;
loginLabel.frame = CGRectMake(0, 0, 271, 37);
[loginLabel removeFromSuperview];
}
}
loginView.delegate = self;
//Add in View
[self.view addSubview:loginView];
///
}
Then After Set Delegate method of Facebook SDK FBLoginView
// This method will be called when the user information has been fetched
- (void)loginViewFetchedUserInfo:(FBLoginView *)loginView
user:(id<FBGraphUser>)user
{
NSLog(@"facebook user Id %@",user.id);
NSLog(@"facebook name=%@",user.name);
NSLog(@"facebook email=%@",[user objectForKey:@"email"]);
NSLog(@"facebook first_name=%@",[user objectForKey:@"first_name"]);
NSLog(@"facebook last_name=%@",[user objectForKey:@"last_name"]);
NSLog(@"facebook id=%@",[user objectForKey:@"id"]);
// to get profile picture add this code
//for image scale use type -> {square,small,normal,large}
NSString *str=[NSString stringWithFormat:@"https://graph.facebook.com/%@/picture?type=normal",[user objectForKey:@"id"]];
NSLog(@"str= %@",str);
//download image from url
UIImage *img=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:str]]];
//set image to Imageview
self.profilePictureView.image=img;
}
- (void)loginViewShowingLoggedInUser:(FBLoginView *)loginView
{
}
// Logged-out user experience
- (void)loginViewShowingLoggedOutUser:(FBLoginView *)loginView
{
NSLog(@"You're not logged in!");
}
// Handle possible errors that can occur during login
- (void)loginView:(FBLoginView *)loginView handleError:(NSError *)error
{
// no action can be done on facebook if there is no session on open state
if (!FBSession.activeSession.isOpen)
{//[@"public_profile", @"email", @"user_friends"
FBSession *session = [[FBSession alloc] initWithPermissions:@[@"publish_stream",@"email",@"user_photos"]];
[FBSession setActiveSession:session];
// Here this behavior garantees that the iOS Login Dialog is not shown, because it was causing some login issues
// The fallback flow will be: 1- Facebook App Native Login Dialog; 2- Facebook App Web Login Dialog; 3- Mobile Safari Login Dialog
[[FBSession activeSession] openWithBehavior:FBSessionLoginBehaviorWithNoFallbackToWebView
completionHandler:^(FBSession *session, FBSessionState state, NSError *error)
{
if(error)
{ // your code here
NSString *alertMessage, *alertTitle;
// If the user should perform an action outside of you app to recover,
// the SDK will provide a message for the user, you just need to surface it.
// This conveniently handles cases like Facebook password change or unverified Facebook accounts.
if ([FBErrorUtility shouldNotifyUserForError:error]) {
alertTitle = @"Facebook error";
alertMessage = [FBErrorUtility userMessageForError:error];
// This code will handle session closures that happen outside of the app
// You can take a look at our error handling guide to know more about it
} else if ([FBErrorUtility errorCategoryForError:error] == FBErrorCategoryAuthenticationReopenSession) {
alertTitle = @"Session Error";
alertMessage = @"Your current session is no longer valid. Please log in again.";
// If the user has cancelled a login, we will do nothing.
// You can also choose to show the user a message if cancelling login will result in
// the user not being able to complete a task they had initiated in your app
// (like accessing FB-stored information or posting to Facebook)
} else if ([FBErrorUtility errorCategoryForError:error] == FBErrorCategoryUserCancelled) {
NSLog(@"user cancelled login");
// For simplicity, this sample handles other errors with a generic message
// You can checkout our error handling guide for more detailed information
} else {
alertTitle = @"Something went wrong";
alertMessage = @"Please try again later.";
NSLog(@"Unexpected error:%@", error);
}
if (alertMessage) {
[[[UIAlertView alloc] initWithTitle:alertTitle
message:alertMessage
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil] show];
}
}
else
{ // your code here
NSLog(@"no error in Login with Facebook..");
}
}];
}
//////-------------------
}