Adding a Facebook Login Button
UIButton *facebook = [UIButton buttonWithType:UIButtonTypeRoundedRect]; facebook.frame = CGRectMake(0, 0, 150,50); facebook.center = self.center; [facebook setTitleEdgeInsets:UIEdgeInsetsMake(0.0f, 30.0f, 0.0f, 0.0f)]; [facebook setTitle:@" Sign in with Facebook " forState:(UIControlStateNormal&UIControlStateHighlighted&UIControlStateSelected)]; [facebook setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted]; [facebook setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; [facebook setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected]; [facebook.titleLabel setFont:[UIFont fontWithName:@"Arial" size:14]]; [facebook addTarget:self action:@selector(facebooklogin:) forControlEvents:UIControlEventTouchDown]; [self.view addSubview:facebook]; -(void)facebooklogin:(id)sender { [[NSUserDefaults standardUserDefaults]setInteger:1 forKey:@"FBonBackGround"]; [self ShowActivityIndicatorWithTitle:@"Loading..."]; Reachability *reach = [Reachability reachabilityForInternetConnection]; NetworkStatus netStatus = [reach currentReachabilityStatus]; if (netStatus == NotReachable) { [self HideActivityIndicator]; UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Connection Failed" message:@"No internet connection available" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil]; [alert show]; } else { if (FBSession.activeSession.isOpen ) { [self UserInformation]; } else { NSArray *permissions = [[NSArray alloc] initWithObjects:@"email",@"user_photos",@"user_birthday",@"read_friendlists", nil]; // NSArray *permissions = [[NSArray alloc] initWithObjects:@"read_friendlists",@"user_photos",nil]; [FBSession openActiveSessionWithReadPermissions:permissions allowLoginUI:YES completionHandler:^(FBSession *session, FBSessionState status, NSError *error) // [FBSession openActiveSessionWithPublishPermissions:permissions defaultAudience:FBSessionDefaultAudienceEveryone allowLoginUI:YES completionHandler:^(FBSession *session, FBSessionState status, NSError *error) { if(!error) { [self UserInformation]; } else { NSLog(@"Error : %@",error); UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Facebook Error" message:@"Picture Guesser uses your Facebook identity to make sure you're real. You'll need to accept to join!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alert show]; [self HideActivityIndicator]; } }]; } } } -(void)UserInformation { Reachability *reach = [Reachability reachabilityForInternetConnection]; NetworkStatus netStatus = [reach currentReachabilityStatus]; if (netStatus == NotReachable) { [self HideActivityIndicator]; UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Connection Failed" message:@"No internet connection available" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil]; [alert show]; } else { [[FBRequest requestForGraphPath:@"me"] startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) { if(!error ) { [self ShowActivityIndicatorWithTitle:@"Loading..."]; [[[FBSession activeSession] accessTokenData] accessToken]; NSLog(@"acces token=%@",[[[FBSession activeSession] accessTokenData] accessToken]); [[NSUserDefaults standardUserDefaults]setValue:[[[FBSession activeSession] accessTokenData] accessToken] forKey:@"FbAccessTokenKey"]; NSLog(@"sdfsdfdsfsf=%@",[[NSUserDefaults standardUserDefaults] valueForKey:@"FbAccessTokenKey"]); NSLog(@"result %@",result); NSString *userName=[result objectForKey:@"name"]; NSString *userid=[result objectForKey:@"id"]; NSString *email = [result objectForKey:@"email"]; NSString *gender = [result objectForKey:@"gender"]; NSString *dob = [result objectForKey:@"birthday"]; NSString *userLocation = [[result objectForKey:@"location"] objectForKey:@"name"]; } }]; } } - (void)ShowActivityIndicatorWithTitle:(NSString *)Title { [SVProgressHUD showWithStatus:Title maskType:SVProgressHUDMaskTypeGradient]; [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.01]]; } - (void)HideActivityIndicator { [SVProgressHUD dismiss]; }In the above code I have used the SVDProgressHUD for displaying the loading indicator. The Reachability class is used for checking the net connection. One thing to take into consideration is that before adding Facebook in iOS programmatically you should first go to facebook developer account and create a facebook app for your respective iOS application. After creating the facebook app for your iOS application you will get an Facebook ID for your app that you will be using in your app to connect with facebook.
thank's for you code..
ReplyDeleteit's very useful for me...
Welcome, I am glad that it helped you.
ReplyDelete