ios - Facebook 通过 ios 中的社交框架登录

标签 ios iphone objective-c facebook-sdk-3.0

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

最佳答案

导入社交和账户框架

将此代码放入您的.m 文件

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

同时在您的设备/模拟器中登录您的 native Facebook 应用程序

关于ios - Facebook 通过 ios 中的社交框架登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20780658/

相关文章:

iphone - 关闭相机 View 会使应用程序崩溃

c++ - 在 iOS 上使用 Android C++ 库

ios - 如何在swift中实现优化的多个计时器?

ios - PhoneGap 推送插件不注册 iOS 8 设备

iphone - UISearchDisplayController 没有结果tableView?

android - Android 和 iOS 中的 C/C++ 标准库在哪里?

iphone - UINavigationBar 左侧有两个按钮

objective-c - HDR 和全景图 : where to learn

ios - 我如何向 CoreData 指示对象已更改?

objective-c - 获取所有已安装应用程序的列表