检查iOS设置中的Facebook / Twitter帐户是否已更改

时间:2014-05-29 09:11:02

标签: ios acaccountstore

我有一个应用程序需要检查存储在iOS设置中的Facebook和Twitter帐户。我可以成功取得这两个。我的问题是如何检查用户是否已删除帐户并在iOS设置中插入新帐户。如果用户返回应用程序,则需要检查用户存储的新帐户。

这是我试图将我的应用程序模式化的应用程序。

enter image description here

2 个答案:

答案 0 :(得分:1)

注意“ACAccountStoreDidChangeNotification”。 例如

- (void)viewDidLoad
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshAccounts:) name:ACAccountStoreDidChangeNotification object:nil];
}

- (void)refreshAccounts:(NSNotification *)note
{
    NSLog(@"Refreshing Accounts");
    //whatever code you want 
}

答案 1 :(得分:0)

以Twitter帐户为例。

如何添加以下功能如下? 找到帐户信息后,您可以再次获取帐户。

- (void)checkTwitterAccountUpdate
{
    ACAccountStore *accountStore = [[ACAccountStore alloc] init];
    ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
    NSArray *accounts = [accountStore accountsWithAccountType:accountType];
    ACAccount *twitterAccount = [accounts lastObject];
    NSString *localTwitterName = [[NSUserDefaults standardUserDefaults] objectForKey:@"kTwitterKey"];
    if([localTwitterName length] > 0)
    {
        if(![twitterAccount.username isEqualToString:localTwitterName])
        {
            NSLog(@"Account did change");
        }
    }
    else
    {
        [accountStore requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error) {
            if(granted) {
                NSArray *accounts = [accountStore accountsWithAccountType:accountType];
                ACAccount *twitterAccount = [accounts lastObject];
                NSLog(@"%@", twitterAccount.username);
                [[NSUserDefaults standardUserDefaults] setObject:twitterAccount.username forKey:@"kTwitterKey"];
                [[NSUserDefaults standardUserDefaults] synchronize];
            }
        }];
    }

}

in

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    [self checkTwitterAccountUpdate];
}
相关问题