iOS Facebook本地登录(不含Facebook SDK) [英] iOS Facebook native login (without Facebook SDK)
问题描述
我正在尝试使用本机(iOS 6-7x)库来授权用户从我的应用程序的Facebook。当登录成功时,我想将认证令牌传递到我的服务器。
I am trying to use native (iOS 6-7x) libraries to authorize a user with Facebook from my app. I would like to pass the auth token to my server when login is successful.
以下代码可以正常工作,除非用户尚未在操作系统中设置自己的Facebook帐户。在这种情况下,我收到以下错误:
The code below works fine except when the user has not set up their Facebook account in the OS. I get the following error in this case:
错误Domain = com.apple.accounts Code = 6操作无法完成(com.apple.accounts error 6。)
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访问?
Do I still need to use the Facebook SDK to ask the user to log in? Is there another iOS native library I could use to prompt the user to set up Facebook access in iOS?
或者,有没有更好的方法来做简化的Facebook认证(不要求用户登录,如果他们已经在操作系统中这样做)?
OR, is there a better way to do streamlined Facebook auth (not asking the user to log in if they have already done so in the OS)?
推荐答案
你应该看看这里的Facebook文档(有一个简单的登录示例):
You should take a look at Facebook docs here (there's a simple login example):
https://developers.facebook.com/docs/ios / ios-sdk-tutorial / authenticate /
在我看来,我将使用Facebook SDK执行登录,可以这样做:
In my opinion, I would use the Facebook SDK to perform the login, you can do something like this:
/* 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中配置Facebook SDK .plist文件。
You have to configure the Facebook SDK in the appDelegate and in the .plist file.
如果用户在设备设置中设置了一个Facebook帐户,您的代码只能工作正常。
Your code only will work fine if the user has a Facebook account set in the device Settings.
I。
这篇关于iOS Facebook本地登录(不含Facebook SDK)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!