Reputation: 3038
I have an application that needs to check for Facebook and Twitter account stored in iOS Settings. I can fetch both successfully. My question is how can I check if the user has deleted the account and inserted a new one in iOS Settings. If the user goes back to the app, it needs to check the new account stored by the user.
This is the app Im trying to pattern my app into.
Upvotes: 0
Views: 691
Reputation: 755
Watch for "ACAccountStoreDidChangeNotification". eg.
- (void)viewDidLoad
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshAccounts:) name:ACAccountStoreDidChangeNotification object:nil];
}
- (void)refreshAccounts:(NSNotification *)note
{
NSLog(@"Refreshing Accounts");
//whatever code you want
}
Upvotes: 1
Reputation: 436
Take twitter account for example.
How about add the following function as below? You can fetch account again after finding account info changed.
- (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];
}
Upvotes: 0