Monday, December 29, 2014

Login With Facebook without Facebook SDK & get Profile Picture of User.

First Import this Library Files in View Controller.
You can Download Library Like "SDWebImage & SVProgressHUD" from GitHub .


#import "UIImageView+WebCache.h"    // Import SDWebImage Library into your Project
#import "UIButton+WebCache.h"
#import <Accounts/Accounts.h>             //Add framework
#import  <Social/Social.h>                     //Add framework
//not compulsory                                                                                                       /*#import "SVProgressHUD.h" */ // Import SVProgressHUD Library into your Project


 #pragma -mark FaceBook

-(void)getFacebookAccount
{
   /* [SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeGradient];*/
  
    if(!_accountStore)
        _accountStore = [[ACAccountStore alloc] init];
  
    ACAccountType *facebookTypeAccount = [_accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
  
    NSArray *fbAccounts =[[NSArray alloc]init];
    fbAccounts= [_accountStore accountsWithAccountType:facebookTypeAccount];
  
    if (![SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook] )
    {
        // set Alert View 
        UIAlertView *alt=[[UIAlertView alloc]initWithTitle:@"Please set Facebook account in Setting" message:@"There is no facebook account set in setting" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil];
        [alt show];
      
       /* [SVProgressHUD dismiss];*/
        return;
    }
  
    NSDictionary *options = [[NSDictionary alloc] initWithObjectsAndKeys:
                             FaceBook_ID, ACFacebookAppIdKey,FBpermition , ACFacebookPermissionsKey,nil];
  
    [_accountStore requestAccessToAccountsWithType:facebookTypeAccount  options:options
                                        completion:^(BOOL granted, NSError *error)
     {
         if(granted)
         {
             NSArray *accounts = [_accountStore accountsWithAccountType:facebookTypeAccount];
             _facebookAccount = [accounts lastObject];
           
             [_accountStore renewCredentialsForAccount:_facebookAccount completion:^(ACAccountCredentialRenewResult renewResult, NSError *error) {
             }];
           
             [self FB_Detail];
         }
         else
         {
            /* [SVProgressHUD dismiss]; */
         }
     }];
}

//Get Facebook Profile Picture

-(void)FB_Detail
{
    NSURL *meurl = [NSURL URLWithString:@"https://graph.facebook.com/me/picture"];
  
  
    SLRequest *merequest = [SLRequest requestForServiceType:SLServiceTypeFacebook
                                              requestMethod:SLRequestMethodGET URL:meurl   parameters:nil];
    merequest.account = _facebookAccount;
  
    [merequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error)
     {
         dispatch_async(dispatch_get_main_queue(), ^{
         // download Image from Facebook
             UIImage *image= [UIImage imageWithData:responseData];
           
             [btnUserProfile setBackgroundImage:image forState:UIControlStateNormal];
            /* [SVProgressHUD dismiss]; */
         });
     }];
}

No comments:

Post a Comment