【问题标题】:iOS Twitter Framework: How to fetch (on the callback) last twitter account used to tweet?iOS Twitter 框架:如何获取(在回调中)最后一个用于发推文的 Twitter 帐户?
【发布时间】:2012-07-18 17:40:33
【问题描述】:

一旦用户发布了推文,我需要区分他使用的是哪个推特帐户。假设用户在手机上配置了 1 个或多个帐户。

我需要在推文成功后知道这一点,所以正确的地方是回调。我尝试使用 ACAccountStore 获取帐户,但它提​​供了一个包含手机上设置的所有帐户的数组,而不是关于最后使用的帐户的线索(甚至不是数组的顺序)。

有谁知道 TWTweetComposeViewController 是否记得这个帐户以及如何获取它?

谢谢

我的代码:

if ([TWTweetComposeViewController canSendTweet])
{
    TWTweetComposeViewController *tweetSheet = 
    [[TWTweetComposeViewController alloc] init];
    [tweetSheet setInitialText:@"initial text"];
    [tweetSheet addImage:[UIImage imageNamed:image]];

    // Callback
    tweetSheet.completionHandler = ^(TWTweetComposeViewControllerResult result) {
        // if tweet was successful
        if(result == TWTweetComposeViewControllerResultDone) {

            // Get the accounts
            account = [[ACAccountStore alloc] init];
            ACAccountType *accountType = [account accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];

            [account requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error) 
             {
                 // if access granted I populate the array
                 if (granted == YES) {
                     NSArray *arrayOfAccounts = [account accountsWithAccountType:accountType];

                     ACAccount *account1 = [arrayOfAccounts objectAtIndex:0];
                     ACAccount *account2 = [arrayOfAccounts objectAtIndex:1];

                     NSString *username1 = account1.username;
                     NSString *username2 = account2.username;

                     // Always same order
                     NSLog(userName1);
                     NSLog(userName2);

                 }
             }];

            [self furtherMethodsInCaseOfSuccessfulTweet];


        } else if(result == TWTweetComposeViewControllerResultCancelled) {
            NSLog(@"twit canceled");
        }
        [self dismissViewControllerAnimated:YES completion:nil];
    };


    [self presentModalViewController:tweetSheet animated:YES];

}
else
{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"No tweet is possible on this device" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
    [alertView show];

}   

}

【问题讨论】:

    标签: ios twitter callback account


    【解决方案1】:

    我找到了办法。

    我们可以使用GET users/lookup 来查询 ACAccountStore 收集的名称,而不是在 TWTweetComposeViewController 的实例中查找用户名。

    http://api.twitter.com/1/users/lookup.xml?screen_name=username1,username2 (使用 json 代替 xml)

    解析结果,我们可以获得用户最后一条推文的日期/时间(“状态”标签),瞧,我们有最后使用的帐户。 另外,您可以在console 上测试qwery 结果。

    感谢@theSeanCook

    【讨论】:

      猜你喜欢
      • 2012-04-25
      • 2012-12-12
      • 2018-02-25
      • 2020-04-16
      • 2012-05-11
      • 2012-06-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多