【问题标题】:Check if Facebook/Twitter account has changed in iOS Settings检查 iOS 设置中的 Facebook/Twitter 帐户是否已更改
【发布时间】:2014-05-29 09:11:02
【问题描述】:

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

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

【问题讨论】:

    标签: ios acaccountstore


    【解决方案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 
    }
    

    【讨论】:

    • 这似乎对我不起作用。你可以发布整个代码。我尝试添加 NSNotification,但当我在“设置”>“FB/Twitter”中删除帐户时,它似乎没有触发。
    • 更新了代码。当您的应用处于后台以允许用户更改设置时,修改对给定社交帐户的访问权限将自动触发此事件并在恢复您的应用之前执行任何代码。
    • 我试过了,但不适合我。只需将 NSLog 放在 refreshAccounts 中,但不会被触发。我会错过什么吗?
    • 确保这个冒号在这里... @selector(refreshAccounts:)
    • 它仍然无法正常工作。将您提供的完全相同的代码放入我的应用程序中的 viewController 中。转到设置> Twitter > 已删除帐户。回到应用程序,没有日志消息。
    【解决方案2】:

    以推特账号为例。

    添加如下功能怎么样? 发现账户信息发生变化后,您可以再次获取账户。

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

    【讨论】:

    • 我只想做一次 requestAccessToAccountsWithType ...也许在那之后存储 ACAccount。但我似乎无法将 ACAccount 存储在 standardUserDefaults 上。
    • 如何将用户名设置为nsuserdefault?刚刚更新了代码。
    猜你喜欢
    • 1970-01-01
    • 2012-01-05
    • 2013-07-23
    • 2014-03-20
    • 2015-07-25
    • 2020-05-11
    • 2014-09-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多