无法使用Parse Objective C登录Facebook

时间:2014-06-17 13:19:53

标签: ios objective-c facebook parse-platform

我在我的应用程序中使用 Parse 。我尝试使用Facebook登录。但我遇到了一个奇怪的问题。如果我从我的iPhone设置中登录Facebook帐户,那么我会收到以下错误。

Error Domain=com.facebook.sdk Code=2 "The operation couldn’t be completed. (com.facebook.sdk error 2.)" UserInfo=0x17560700 {com.facebook.sdk:ErrorLoginFailedReason=com.facebook.sdk:SystemLoginCancelled, com.facebook.sdk:ErrorInnerErrorKey=Error Domain=com.apple.accounts Code=7 "The operation couldn’t be completed. (com.apple.accounts error 7.)", com.facebook.sdk:ErrorSessionKey=<FBSession: 0x175227e0, state: FBSessionStateClosedLoginFailed, loginHandler: 0x0, appID: 638916992864352, urlSchemeSuffix: , tokenCachingStrategy:<PFFacebookTokenCachingStrategy: 0x175a9900>, expirationDate: (null), refreshDate: (null), attemptedRefreshDate: 0001-12-30 00:00:00 +0000, permissions:(null)>}

我从iPhone的设置 - &gt; Facebook退出,然后工作正常。

我已经对它进行过研究,但没有得到解决方案。

任何人都可以帮忙解决我的问题吗?

谢谢,

1 个答案:

答案 0 :(得分:0)

尝试使用以下代码。您需要在代码中授予权限,如下面的代码..

-(IBAction)facebookButtonAction:(id)sender
{
   if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook])
{

    self.accountStore = [[ACAccountStore alloc] init];


    // Create an account type that ensures Twitter accounts are retrieved.
    ACAccountType *accountType = [self.accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
    NSArray * permissions = [NSArray arrayWithObjects:@"email",@"user_location",@"user_birthday", nil];
    NSDictionary * dict=[NSDictionary dictionaryWithObjectsAndKeys:@“xxxxxxxxxxxxxxx,ACFacebookAppIdKey,permissions,ACFacebookPermissionsKey,ACFacebookAudienceEveryone,ACFacebookAudienceKey, nil];

    [self.accountStore requestAccessToAccountsWithType:accountType options:dict completion:^(BOOL granted, NSError *error) {

        NSLog(@"-------------error =%@---------  check grant bol %d",error,granted);

        if(granted ) {
            NSArray *accountsArray = [self.accountStore accountsWithAccountType:accountType];

            if ([accountsArray count] > 0)
            {

                ACAccount *fbAccount = [accountsArray objectAtIndex:0];

                NSDictionary *tempDict = [[NSMutableDictionary alloc] initWithDictionary:[fbAccount dictionaryWithValuesForKeys:[NSArray arrayWithObject:@"properties"]]];

                NSString *facebookname;

                facebookname=@"Name not found";

                 facebookname = [[tempDict objectForKey:@"properties"] objectForKey:@"ACPropertyFullName"];

                if ([facebookname isKindOfClass:[NSNull class]] || facebookname==NULL || [facebookname isEqualToString:@""] || [facebookname isEqualToString:@"(null)"])
                {
                     facebookname = [[tempDict objectForKey:@"properties"] objectForKey:@"fullname"];
                }
                //if (SYSTEM_VERSION_LESS_THAN(@"7.0"))
                {

                }


                NSString *facebookID = [NSString stringWithFormat:@"%@", [[fbAccount valueForKey:@"properties"] valueForKey:@"uid"]] ;

                NSString *pictureURL=[NSString stringWithFormat:@"https://graph.facebook.com/%@/picture?type=large&return_ssl_resources=1",facebookID];

                NSLog(@" check the properties %@",tempDict);
               NSLog(@" name nemae %@",facebookname);
                 NSLog(@" facebookID %@",facebookID);
                NSLog(@" image  https://graph.facebook.com/%@/picture?type=large&return_ssl_resources=1",facebookID);


                if ([facebookname isKindOfClass:[NSNull class]] || facebookname==NULL || [facebookname isEqualToString:@""] || [facebookname isEqualToString:@"(null)"])
                {
                    UIAlertView *somethingerr=[[UIAlertView alloc]initWithTitle:@“App Name” message:@"Something went wrong with facebook login. Please send your console report to the developers." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
                    [somethingerr show
                     ];

                    [self hidespinner];

                    return;
                }

            }

            else{
                UIAlertView *alerts=[[UIAlertView alloc]initWithTitle:@"No facebok account" message:@"Please add or create an account in \"settings \" " delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
                [alerts show];
                [self performSelector:@selector(hidespinner) withObject:nil afterDelay:0.1] ;

            }
        }

        else{

            switch ([error code])
            {
                case 0:
                    [self showErrorAlertWithMessage:@"Unknown error occured, try again later!"];
                    break;

                case 1:
                    [self showErrorAlertWithMessage:@"Unknown error occured, try again later!"];
                    break;
                case 3:
                    [self showErrorAlertWithMessage:@"Authentication failed, try again later!"];
                    break;
                case 6:
                    [self showErrorAlertWithMessage:@"Facebook account does not exists. Please create it in Settings and come back!"];
                    break;
                case 7:
                    [self showErrorAlertWithMessage:@"Permission request failed. You won't be able to share information to Facebook"];
                    break;
                default:
                    [self performSelector:@selector(hidespinner) withObject:Nil afterDelay:0.1];
                    break;
            }

        }

    }];


}
else{

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No Facebook Accounts" message:@"There are no Facebook accounts configured. You can add or create a Facebook account in Settings." delegate:nil                                                       cancelButtonTitle:@"OK"  otherButtonTitles:nil];

    [alert show];
    [self performSelector:@selector(hidespinner) withObject:nil afterDelay:0.1] ;

}
}

希望它可以帮助你....!