Facebook通过ios中的社交框架登录 [英] Facebook login via social framework in ios

查看:118
本文介绍了Facebook通过ios中的社交框架登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是iOS开发的新手。我必须在我的iOS应用程序中集成Facebook登录,因为用户登录它会获取所有信息并将其导航到主屏幕。我通过Facebook最新的SDK完成了这个,但我希望它通过社交框架完成。我已经浏览了很多在线资料,并且还找到了他们通过社交框架在Facebook页面(Wall)上分享(发布)内容的示例,但不是Facebook登录。任何人请帮助,让我知道如果Facebook可以通过社交框架登录,或者我必须坚持使用SDK。在此先感谢

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

推荐答案

导入社交和帐户框架

把这个放.m文件中的代码

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);
        }
    }];
}

还记录在设备/模拟器中的原生Facebook应用程序中

这篇关于Facebook通过ios中的社交框架登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆