我有一个应用程序需要检查 iOS 设置中存储的 Facebook 和 Twitter 帐户。我可以成功地获取两者。我的问题是如何检查用户是否删除了该帐户并在 iOS 设置中插入了一个新帐户。如果用户回到应用程序,它需要检查用户存储的新帐户。
这是我试图将我的应用程序模式化的应用程序。
我有一个应用程序需要检查 iOS 设置中存储的 Facebook 和 Twitter 帐户。我可以成功地获取两者。我的问题是如何检查用户是否删除了该帐户并在 iOS 设置中插入了一个新帐户。如果用户回到应用程序,它需要检查用户存储的新帐户。
这是我试图将我的应用程序模式化的应用程序。
注意“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
}
以推特账号为例。
添加如下功能怎么样?发现账户信息发生变化后,您可以再次获取账户。
- (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];
}
}];
}
}
在
- (void)applicationDidBecomeActive:(UIApplication *)application
{
[self checkTwitterAccountUpdate];
}