本文介绍了何时应该续订ACAccount?或者,如何检查凭证是否过期。 (Facebook)的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我被调查了如何使用iOS框架ACAccount和Social来实现Facebook的功能。如果(!_ accountStore)
_accountStore = [/ b]
$ b pre $ [ACAccountStore alloc] init];

ACAccountType * facebookTypeAccount = [_accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];

//检查是否有任何faceboook帐户
NSArray * accounts = [_accountStore accountsWithAccountType:facebookTypeAccount];
if(![accounts count]){
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@alert消息:@在设置中没有配置Facebook帐户。委托:nil cancelButtonTitle:@OKotherButtonTitles:nil];
[alert show];
返回;
}

[_accountStore requestAccessToAccountsWithType:facebookTypeAccount
选项:@ {ACFacebookAppIdKey:@FACEBOOK-APP-ID,ACFacebookPermissionsKey:@ [@email]}
完成:^(BOOL授予,NSError *错误){
如果(已授权){
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访问被拒绝,请在Setting-> Facebook中授予访问权限错误消息::%@,错误] ;
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@Alertmessage:message delegate:nil cancelButtonTitle:@OKotherButtonTitles:nil];
[alert show];
});
}
}];

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

   - (IBAction)postButtonPressed:(id)sender {
if(!_facebookAccount){
UIAlertView * alert = [[ UIAlertView alloc] initWithTitle:@alert消息:@login first委托:nil cancelButtonTitle:@OKotherButtonTitles:nil];
[alert show];
返回;
}

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(@发布....);
}
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 *)帐户完成:(ACAccountStoreCredentialRenewalHandler)completionHandler; 

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



哦,顺便说一下,我们只想使用本机社交框架来做简单的工作,如发布一些消息或图片。所以,如果不需要,我们不会使用Facebook SDK。



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



更新2013.11.20 11:10 AM



我从我的实验中学到了这个问题。


  1. 一个人无法获得某种类型的帐户在帐户存储之前,他获得访问权限,所以我不应该在请求访问之前检查帐号。


  2. 当使用ACAccount的应用程序处于后台帐户更改后的背景。目前,我只看到访问权限的更改触发了通知。


  3. 如果用户更改密码,系统会在用户尝试发布时弹出警告某些东西要求用户更改密码。


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

解决方案

您应该在每次不同步时续订用户acc。如果用户已经更改了密码或acc会话已过期,可能会发生这种情况。



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




Recently I was assigned to survey how to use iOS framework ACAccount and Social to implement facebook post function. It is quite simple to gain access of the account configured in setting.

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];
                                            });
                                        }
                                    }];

Once the app gain access to facebook, it can post message by using 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");
}

Here comes my question. I found that there is a method in ACAccountStore which is used to renew expired credential of an ACAccount:

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

But I don't even know how to check whether the credential is expired so that I can renew it. Anyone got an idea about this?

Oh, by the way, we just want to use the native Social framework to do simple work such as post some message or picture. So, if not needed, we are not going to use the facebook SDK.

If you know anything about how to check the credential is valid or not, please leave a comment or submit an answer, thank you:)

Updates 2013.11.20 11:10 AM

I learn something from my experiments to this issue..

  1. One is not able to get certain type of accounts from account store before he gains access to them, so I should not check account count before request for access.

  2. Renew notification called when the app using ACAccount is in background after facebook account changed. Currently, I only saw changes of access right triggers the notification.

  3. If the user changes password, the system will pop out an alert when the user attempt to post something, which ask the user to change password.

I think monitor notifications of account change is enough to handle the changes. I'll accept the first answer.

解决方案

You should renew the user acc everytime it is out of sync. This may happen if the user has changed his password or when the acc session has expired.

Yo can know you are in that scenario using the following notification:

ACAccountStoreDidChangeNotification

这篇关于何时应该续订ACAccount?或者,如何检查凭证是否过期。 (Facebook)的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-27 23:38