gpt4 book ai didi

facebook - iOS Facebook 原生登录(不带 Facebook SDK)

转载 作者:行者123 更新时间:2023-11-30 05:23:01 26 4
gpt4 key购买 nike

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

下面的代码工作正常,除非用户没有在操作系统中设置他们的 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 native 库来提示用户在 iOS 中设置 Facebook 访问权限吗?

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

最佳答案

你应该看看这里的 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 帐户时,您的代码才能正常工作。

我.

关于facebook - iOS Facebook 原生登录(不带 Facebook SDK),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19853865/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com