6

我正在尝试使用本机(iOS 6-7x)库从我的应用程序授权用户使用 Facebook。登录成功后,我想将身份验证令牌传递给我的服务器。

下面的代码可以正常工作,除非用户没有在操作系统中设置他们的 Facebook 帐户。在这种情况下,我收到以下错误:

错误 Domain=com.apple.accounts Code=6 “操作无法完成。(com.apple.accounts 错误 6。)

-(void) initFacebookLogin
{
    LRAppDelegate *appDelegate = [[UIApplication sharedApplication]delegate];

    if (appDelegate.accountStore == nil)
        appDelegate.accountStore = [[ACAccountStore alloc] init];

    __block ACAccount *facebookAccount = nil;

    ACAccountType *facebookAccountType = [appDelegate.accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];

    NSArray * permissions = @[@"publish_stream", @"publish_actions", @"email"];

    NSMutableDictionary *options = [[NSMutableDictionary alloc] initWithObjectsAndKeys:FACEBOOK_APP_ID, ACFacebookAppIdKey, permissions, ACFacebookPermissionsKey, ACFacebookAudienceOnlyMe, ACFacebookAudienceKey, nil];

    [appDelegate.accountStore requestAccessToAccountsWithType:facebookAccountType
        options: options
        completion: ^(BOOL granted, NSError *error) {

           if ( granted )
           {
               NSArray *accounts = [appDelegate.accountStore accountsWithAccountType:facebookAccountType];

               facebookAccount = [accounts lastObject];

               ACAccountCredential* accountCredential = [facebookAccount credential];

               NSString* token = [accountCredential oauthToken];

               NSLog( @"token=%@", token );
           }
           else {
               // WHAT DO I DO HERE???
               // Error Domain=com.apple.accounts Code=6 "The operation couldn’t be completed. (com.apple.accounts error 6.)"
               NSLog(@"%@", [error description]);
           }
       }];
}

我还需要使用 Facebook SDK 要求用户登录吗?我可以使用另一个 iOS 原生库来提示用户在 iOS 中设置 Facebook 访问吗?

或者,是否有更好的方法来简化 Facebook 身份验证(如果用户已经在操作系统中登录,则不要求用户登录)?

4

1 回答 1

0

您应该在这里查看 Facebook 文档(有一个简单的登录示例):

https://developers.facebook.com/docs/ios/ios-sdk-tutorial/authenticate/

在我看来,我会使用 Facebook SDK 来执行登录,你可以这样做:

/* Constants */
#define K_FACEBOOK_REQUEST_CREDENTIALSURL @"me/?fields=id,location,name,username,bio,picture,gender,website,birthday,email"
#define K_FACEBOOK_PERMISSIONS @[@"user_about_me",@"user_birthday",@"email"]

/* Facebook Keys */
#define K_FACEBOOK_REQUEST_ID @"id"
#define K_FACEBOOK_REQUEST_USERNAME @"username"
#define K_FACEBOOK_REQUEST_LOCATION @"location"
#define K_FACEBOOK_REQUEST_FULLNAME @"name"
#define K_FACEBOOK_REQUEST_BIO @"bio"
#define K_FACEBOOK_REQUEST_WEBSITE @"website"
#define K_FACEBOOK_REQUEST_EMAIL @"email"
#define K_FACEBOOK_REQUEST_BIRTHDAY @"birthday"
#define K_FACEBOOK_REQUEST_GENDER @"gender"

- (void)initWithLoginFacebook
{
  [PFFacebookUtils logInWithPermissions:K_FACEBOOK_PERMISSIONS block:^(PFUser *user, NSError *error) {
    if (!user) {
        if (!error) 
          NSLog("ERROR_CAUSE_AUTH_USERCANCELLED");
        else 
          NSLog("ERROR_CAUSE_AUTH_FAILEDLOGIN");
    } else if (user.isNew) {
        FBRequest *request = [FBRequest requestForMe];
        [request startWithCompletionHandler:^(FBRequestConnection *connection,id result,NSError *error) {
            if (error) 
              NSLog("ERROR_CAUSE_AUTH_FAILEDLOGIN");
            else 
              [self requestLoginFacebook:result];
        }];
    } else
        NSLog("User logged and not new!");
  }];
}

- (void)requestLoginFacebook:(id)result
 {
    NSDictionary *userData = (NSDictionary *)result;
    // Do something with the data... For example
    NSString *facebookId = userData[K_FACEBOOK_REQUEST_ID];
    NSString *name = userData[K_FACEBOOK_REQUEST_FULLNAME];
    NSString *username = [userData[K_FACEBOOK_REQUEST_USERNAME] lowercaseString];
    NSString *bio = userData[K_FACEBOOK_REQUEST_BIO];
    NSString *website = userData[K_FACEBOOK_REQUEST_WEBSITE];
    NSString *location = userData[K_FACEBOOK_REQUEST_LOCATION][@"name"];
    NSString *gender = userData[K_FACEBOOK_REQUEST_GENDER];
    NSString *birthday = userData[K_FACEBOOK_REQUEST_BIRTHDAY];
    NSString *email = userData[K_FACEBOOK_REQUEST_EMAIL];
    NSString *profileImage = [NSString stringWithFormat:@"https://graph.facebook.com/%@/picture?width=180&height=180",userData[@"id"]];

}

您必须在 appDelegate 和 .plist 文件中配置 Facebook SDK。

只有当用户在设备设置中设置了 Facebook 帐户时,您的代码才能正常工作。

我。

于 2013-11-08T11:40:35.207 回答