我使用以下教程使用新的 6.0 Facebook 集成进行登录 。iOS 6 教程:将 Facebook 集成到您的应用程序中
在这里,我不需要 Facebook SDK 只是来自苹果的新社交框架,它运行良好。
我的问题是如何在情节提要中构建这个单点登录?在成功验证后,我的登录视图应该将用户重定向到下一个视图,并且永远不会再次出现。
我一直在寻找一个准备序列的解决方案,但不确定我如何捕获用户的登录状态,以及下次用户打开应用程序时如何检查它。
当按下登录按钮时,会触发以下功能:
- (IBAction)advancedButtonPressed:(id)sender {
self.accountStore = [[ACAccountStore alloc]init];
ACAccountType *FBaccountType= [self.accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
NSString *key = @"270430619733847";
NSDictionary *dictFB = [NSDictionary dictionaryWithObjectsAndKeys:key,ACFacebookAppIdKey,@[@"email"],ACFacebookPermissionsKey, nil];
[self.accountStore requestAccessToAccountsWithType:FBaccountType options:dictFB completion:
^(BOOL granted, NSError *e) {
if (granted) {
NSArray *accounts = [self.accountStore accountsWithAccountType:FBaccountType];
//it will always be the last object with single sign on
self.facebookAccount = [accounts lastObject];
NSLog(@"facebook account =%@",self.facebookAccount);
[self get];
} else {
//Fail gracefully...
NSLog(@"error getting permission %@",e);
}
}];
获取方法是:
-(void)get
{
NSURL *requestURL = [NSURL URLWithString:@"https://graph.facebook.com/me"];
SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodGET
URL:requestURL
parameters:nil];
request.account = self.facebookAccount;
[request performRequestWithHandler:^(NSData *data,
NSHTTPURLResponse *response,
NSError *error) {
if(!error)
{
list =[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSLog(@"Dictionary contains: %@", list );
if([list objectForKey:@"error"]!=nil)
{
[self attemptRenewCredentials];
}
dispatch_async(dispatch_get_main_queue(),^{
nameLabel.text = [list objectForKey:@"username"];
});
}
else{
//handle error gracefully
NSLog(@"error from get%@",error);
//attempt to revalidate credentials
}
}];