如果用户在他的 iPhone 设置中设置了 facebook 帐户,我可以获得用户个人资料权限。为此,我使用了以下代码:
if(!_accountStore)
_accountStore = [[ACAccountStore alloc] init];
ACAccountType *facebookTypeAccount = [_accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
[_accountStore requestAccessToAccountsWithType:facebookTypeAccount
options:@{ACFacebookAppIdKey: @"app_id", ACFacebookPermissionsKey: @[@"email"]}
completion:^(BOOL granted, NSError *error) {
if(granted){
NSArray *accounts = [_accountStore accountsWithAccountType:facebookTypeAccount];
_facebookAccount = [accounts lastObject];
NSLog(@"Success");
[self me];
}else{
NSLog(@"go to iphone settings");
}
}];
-(void)me
{
NSURL *meurl = [NSURL URLWithString:@"https://graph.facebook.com/me"];
SLRequest *merequest = [SLRequest requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodGET
URL:meurl
parameters:nil];
merequest.account = _facebookAccount;
[merequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSString *meDataString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(@"%@", meDataString);
}];
}
如果用户没有设置他的 facebook 帐户,那么控件将移至其他部分,即转到 iphone 设置。在其他部分,我想打开 facebook 登录页面,这样用户就可以在不进入 iPhone 设置的情况下登录他的 facebook 帐户。那么我们如何通过 SDK 打开 facebook 登录。请帮我解决这个问题。
提前致谢。
最佳答案
在这种情况下,如果您没有使用用户帐户(如果他/她没有在 iOS 设置中设置它),您需要在您的应用程序上实现 Facebook SDK ( https://developers.facebook.com/ios/ ) .很简单,只需按照以下步骤操作 ( https://developers.facebook.com/docs/getting-started/facebook-sdk-for-ios/ )。
之后就可以使用FBSession的
方法+openActiveSessionWithReadPermissions:allowLoginUI:completionHandler:
来进行用户登录了。
说实话,在我的应用程序中,我什至不尝试使用 Apple 的 requestAccessToAccountsWithType:options:completion:;
和最新的 Facebook SDK 登录用户,默认行为是:
- 尝试使用 iOS 设置中设置的用户帐户登录;
- 如果没有找到帐户,尝试打开 Facebook 应用程序以请求登录/权限;
- 如果设备上未安装 Facebook 应用程序,它会在 Safari 上打开 Facebook,作为备用。
关于iphone - 使用 SDK 登录 facebook,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16208140/