iOS Facebook原生登录(无需使用Facebook SDK)

6

我正在尝试使用本地(iOS 6-7x)库从我的应用程序授权用户访问Facebook。如果登录成功,我想将auth令牌传递给我的服务器。

以下代码在用户未在操作系统中设置其Facebook帐户时除外,工作正常。在这种情况下,我会收到以下错误:

Error Domain=com.apple.accounts Code=6 "The operation couldn’t be completed. (com.apple.accounts error 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认证(如果用户已经在操作系统中登录,则不要求用户再次登录)?

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帐户时,您的代码才能正常工作。

I.


谢谢。如果用户已经在Facebook或其他应用程序中使用本机FB登录进行了身份验证,那么这不会强制用户输入凭据吗?这就是我要找的。 - dixkin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接