Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facebook login via social framework in ios

I'm new with iOS development. I have to integrate Facebook login in my iOS app, as user logs in it fetches all the info and navigates it to home screen. I have done this via Facebook latest SDK but I want it to be done via social framework. I have gone through lots of online stuff and also have found examples where they are sharing(posting) content on Facebook page(Wall) via social framework but not Facebook login exactly. Anyone please help, let me know if Facebook login is possible via social framework or i have to stick to SDK. Thanks in Advance

like image 827
Arun Avatar asked Dec 26 '13 07:12

Arun


People also ask

How do I log into Facebook on iOS?

To start, go to the home screen on your iPhone (Image 1). Click on the dark blue icon with a white “f” on it to access your Facebook account (this is the Facebook application icon). Type in your, previously established, E-mail address and your Facebook password to log into your account (Image 2).

What is Facebook SDK for iOS?

The Facebook SDK enables: Facebook Login - Authenticate people with their Facebook credentials. Share and Send dialogs - Enable sharing content from your app to Facebook. App Events - Log events in your application.


1 Answers

Import Social and Acount Framwork

put this code in your .m file

-(void)facebook
{
    self.accountStore = [[ACAccountStore alloc]init];
    ACAccountType *FBaccountType= [self.accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];

    NSString *key = @"451805654875339";
    NSDictionary *dictFB = [NSDictionary dictionaryWithObjectsAndKeys:key,ACFacebookAppIdKey,@[@"email"],ACFacebookPermissionsKey, nil];

    [self.accountStore requestAccessToAccountsWithType:FBaccountType options:dictFB completion:
     ^(BOOL granted, NSError *e) {
         if (granted)
         {
             NSArray *accounts = [self.accountStore accountsWithAccountType:FBaccountType];
             //it will always be the last object with single sign on
             self.facebookAccount = [accounts lastObject];


             ACAccountCredential *facebookCredential = [self.facebookAccount credential];
             NSString *accessToken = [facebookCredential oauthToken];
             NSLog(@"Facebook Access Token: %@", accessToken);


             NSLog(@"facebook account =%@",self.facebookAccount);

             [self get];

             [self getFBFriends];

             isFacebookAvailable = 1;
         } else
         {
             //Fail gracefully...
             NSLog(@"error getting permission yupeeeeeee %@",e);
             sleep(10);
             NSLog(@"awake from sleep");
             isFacebookAvailable = 0;

         }
     }];



}

-(void)checkfacebookstatus
{
    if (isFacebookAvailable == 0)
    {
        [self checkFacebook];
        isFacebookAvailable = 1;
    }
    else
    {
        printf("Get out from our game");
    }
}


-(void)get
{

    NSURL *requestURL = [NSURL URLWithString:@"https://graph.facebook.com/me"];

    SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeFacebook requestMethod:SLRequestMethodGET URL:requestURL parameters:nil];
    request.account = self.facebookAccount;

    [request performRequestWithHandler:^(NSData *data, NSHTTPURLResponse *response, NSError *error) {

        if(!error)
        {

            NSDictionary *list =[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];

            NSLog(@"Dictionary contains: %@", list );




            self.globalmailID   = [NSString stringWithFormat:@"%@",[list objectForKey:@"email"]];
            NSLog(@"global mail ID : %@",globalmailID);


            fbname = [NSString stringWithFormat:@"%@",[list objectForKey:@"name"]];
            NSLog(@"faceboooookkkk name %@",fbname);




            if([list objectForKey:@"error"]!=nil)
            {
                [self attemptRenewCredentials];
            }
            dispatch_async(dispatch_get_main_queue(),^{

            });
        }
        else
        {
            //handle error gracefully
            NSLog(@"error from get%@",error);
            //attempt to revalidate credentials
        }

    }];

    self.accountStore = [[ACAccountStore alloc]init];
    ACAccountType *FBaccountType= [self.accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];

    NSString *key = @"451805654875339";
    NSDictionary *dictFB = [NSDictionary dictionaryWithObjectsAndKeys:key,ACFacebookAppIdKey,@[@"friends_videos"],ACFacebookPermissionsKey, nil];


    [self.accountStore requestAccessToAccountsWithType:FBaccountType options:dictFB completion:
     ^(BOOL granted, NSError *e) {}];

}




-(void)accountChanged:(NSNotification *)notification
{
    [self attemptRenewCredentials];
}

-(void)attemptRenewCredentials
{
    [self.accountStore renewCredentialsForAccount:(ACAccount *)self.facebookAccount completion:^(ACAccountCredentialRenewResult renewResult, NSError *error){
        if(!error)
        {
            switch (renewResult) {
                case ACAccountCredentialRenewResultRenewed:
                    NSLog(@"Good to go");
                    [self get];
                    break;
                case ACAccountCredentialRenewResultRejected:
                    NSLog(@"User declined permission");
                    break;
                case ACAccountCredentialRenewResultFailed:
                    NSLog(@"non-user-initiated cancel, you may attempt to retry");
                    break;
                default:
                    break;
            }

        }
        else{
            //handle error gracefully
            NSLog(@"error from renew credentials%@",error);
        }
    }];
}

Also logged in your native Facebook application in your device/Simulator

like image 92
Gajendra Rawat Avatar answered Oct 10 '22 15:10

Gajendra Rawat