何时更新ACAccount? 或者,如何检查凭证是否过期。 (Facebook)的

最近我被分配到调查如何使用iOS框架ACAccount和社会实施Facebook的发布function。 访问设置中configuration的帐户非常简单。

if(!_accountStore) _accountStore = [[ACAccountStore alloc] init]; ACAccountType *facebookTypeAccount = [_accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook]; // Check if there is any faceboook account NSArray *accounts = [_accountStore accountsWithAccountType:facebookTypeAccount]; if (![accounts count]) { UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"alert" message:@"No facebook account configured in setting." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alert show]; return; } [_accountStore requestAccessToAccountsWithType:facebookTypeAccount options:@{ACFacebookAppIdKey: @"FACEBOOK-APP-ID", ACFacebookPermissionsKey: @[@"email"]} completion:^(BOOL granted, NSError *error) { if(granted){ NSArray *accounts = [_accountStore accountsWithAccountType:facebookTypeAccount]; _facebookAccount = [accounts lastObject]; NSLog(@"Success"); }else{ // ouch NSLog(@"Failed, Error: %@", error); dispatch_async(dispatch_get_main_queue(), ^{ NSString *message = [NSString stringWithFormat:@"App access denied, please grant access in Setting->Facebook. Error message::%@", error]; UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Alert" message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alert show]; }); } }]; 

一旦应用程序获得访问Facebook,它可以通过使用SLComposeViewController发布消息:

 - (IBAction)postButtonPressed:(id)sender { if (!_facebookAccount) { UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"alert" message:@"login first" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alert show]; return; } SLComposeViewController *fbController = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook]; if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) { SLComposeViewControllerCompletionHandler __block completionHandler=^(SLComposeViewControllerResult result){ [fbController dismissViewControllerAnimated:YES completion:nil]; switch(result){ case SLComposeViewControllerResultCancelled: default: { NSLog(@"Cancelled....."); } break; case SLComposeViewControllerResultDone: { NSLog(@"Posted...."); } break; }}; [fbController setInitialText:@"Test message"]; [fbController setCompletionHandler:completionHandler]; [self presentViewController:fbController animated:YES completion:nil]; } else { NSLog(@"no facebook setup"); } 

这是我的问题。 我发现ACAccountStore中有一个用于更新ACAccount过期凭证的方法:

 - (void)renewCredentialsForAccount:(ACAccount *)account completion:(ACAccountStoreCredentialRenewalHandler)completionHandler; 

但我甚至不知道如何检查证书是否过期,以便我可以更新。 任何人都有这个想法?

哦,顺便说一句,我们只是想使用本地的社会框架来做简单的工作,如张贴一些消息或图片。 所以,如果不需要的话,我们不会使用facebook SDK。

如果您知道如何检查凭证是否有效,请留下评论或提交答案,谢谢:)

更新2013.11.20 11:10 AM

我从我的实验中学到了一些东西。

  1. 在获得访问权限之前,无法从帐户存储中获得某种types的帐户,所以在请求访问之前,我不应该检查帐户数量。

  2. 更新Facebook帐号后,使用ACAccount的应用在后台时更新通知。 目前,我只看到访问权限的变化触发通知。

  3. 如果用户更改密码,当用户尝试发布某些内容时,系统会popup提示,要求用户更改密码。

我认为监视器帐户更改的通知足以处理更改。 我会接受第一个答案。

每当它不同步时,您应该更新用户acc。 如果用户更改密码或会话过期,可能会发生这种情况。

哟可以知道你在这种情况下使用以下通知:

ACAccountStoreDidChangeNotification

我不认为有积极的方式,最好是写更新函数,并调用renewCredentialsForAccount框架