【问题标题】:List of twitter accounts code works in iOS 6 and not in iOS 7?twitter 帐户代码列表适用于 iOS 6 而不是 iOS 7?
【发布时间】:2014-01-14 18:14:59
【问题描述】:

为什么下面的代码 works fine in ios6not in ios7 ?我错过了ios 7的东西吗

ACAccountStore *account_Store = [[ACAccountStore alloc] init];
ACAccountType *account_Type = [account_Store     accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
 arrayOfAccounts = [[NSMutableArray alloc]initWithArray:[account_Store accountsWithAccountType:account_Type]];


NSLog(@"arrayOfAccounts %@",arrayOfAccounts); 


if ([arrayOfAccounts count] > 0)
{
    ACAccount *acct1;
    array = [[NSMutableArray alloc]init];

    for (int i = 0 ; i < [arrayOfAccounts count]; i++ )
    {
        acct1 = [[ACAccount alloc]init];
        acct1 = [arrayOfAccounts objectAtIndex:i];
        [array setObject:[NSString stringWithFormat:@"%@",acct1.username] atIndexedSubscript:i];
        // NSString *username = acct1.username;
        // NSLog(@"user at index %d : username %@",i,username);

    }
}

Debug 区域中的这条语句NSLog(@"arrayOfAccounts %@",arrayOfAccounts); 给出了输出:

arrayOfAccounts ( )

【问题讨论】:

  • 这是在模拟器还是手机上?您是否在 iOS 7 模拟器上设置了 Twitter 帐户?
  • 是的,我已经在模拟器和设备上进行了测试,并且我也从设置中设置了 twitter 帐户

标签: ios twitter ios6 ios7


【解决方案1】:

我以前这样做是为了获取帐户数组,它确实在 ios 6 和 ios 7 上都给了我。 试一试,如果它的工作。谢谢。

在你的 .h 文件中导入这些框架

#import <Twitter/Twitter.h>
#import <Accounts/Accounts.h>

将此 ARC 兼容代码添加到您的 .m 文件中的任何按钮操作或您想要的帐户数组中 --

  /*

// working code for ios 6 and ios 7

if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1)
{
   // ios 6 working code
}
else 
{
  // ios 7 working code

}

 */

  ACAccountStore *account_Store = [[ACAccountStore alloc] init];
ACAccountType *account_Type = [account_Store accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];


if (!account_Type)
{

    UIAlertView *alert1 = [[UIAlertView alloc]initWithTitle:@"Twitter" message:@"Permission not granted" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    alert1.tag = 888;
    [alert1 show];
    return;
}


if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1)
{
    [arrayOfAccounts removeAllObjects];
    [array removeAllObjects];
    arrayOfAccounts = [[NSMutableArray alloc]initWithArray:[account_Store accountsWithAccountType:account_Type]];
    NSLog(@"arrayOfAccounts %@",arrayOfAccounts);

    if ([arrayOfAccounts count] > 0)
    {
        ACAccount *acct1;
        array = [[NSMutableArray alloc]init];

        for (int i = 0 ; i < [arrayOfAccounts count]; i++ )
        {
            acct1 = [[ACAccount alloc]init];
            acct1 = [arrayOfAccounts objectAtIndex:i];
            [array setObject:[NSString stringWithFormat:@"%@",acct1.username] atIndexedSubscript:i];
            // NSString *username = acct1.username;
            // NSLog(@"user at index %d : username %@",i,username);

        }

        //  NSLog(@"array %@",array);

        UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Select your Twitter Account"
                                                                 delegate:self
                                                        cancelButtonTitle:nil
                                                   destructiveButtonTitle:nil
                                                        otherButtonTitles:nil];

        // ObjC Fast Enumeration
        for (int i = 0 ; i < [array count]; i++)
        {
            [actionSheet addButtonWithTitle:[array objectAtIndex:i]];
        }

        [actionSheet addButtonWithTitle:@"Cancel"];
        actionSheet.cancelButtonIndex = [array count];

        [actionSheet showInView:self.view];
    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Twitter" message:@"You have no twitter account" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        alert.tag = 999;

        [alert show];

    }



}
else // ios 7 working code
{


[account_Store requestAccessToAccountsWithType:account_Type options:nil completion:^(BOOL granted, NSError *error)
{
    if (granted)
    {
            dispatch_sync(dispatch_get_main_queue(), ^{

            arrayOfAccounts = [[NSMutableArray alloc]initWithArray:[account_Store accountsWithAccountType:account_Type]];
            NSLog(@"arrayOfAccounts %@",arrayOfAccounts);

            if ([arrayOfAccounts count] > 0)
            {
                ACAccount *acct1;
                array = [[NSMutableArray alloc]init];

                for (int i = 0 ; i < [arrayOfAccounts count]; i++ )
                {
                    acct1 = [[ACAccount alloc]init];
                    acct1 = [arrayOfAccounts objectAtIndex:i];
                    [array setObject:[NSString stringWithFormat:@"%@",acct1.username] atIndexedSubscript:i];
                    // NSString *username = acct1.username;
                    // NSLog(@"user at index %d : username %@",i,username);

                }

                //  NSLog(@"array %@",array);

                UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Select your Twitter Account"
                                                                         delegate:self
                                                                cancelButtonTitle:nil
                                                           destructiveButtonTitle:nil
                                                                otherButtonTitles:nil];

                // ObjC Fast Enumeration
                for (int i = 0 ; i < [array count]; i++)
                {
                    [actionSheet addButtonWithTitle:[array objectAtIndex:i]];
                }

                [actionSheet addButtonWithTitle:@"Cancel"];
                actionSheet.cancelButtonIndex = [array count];

                [actionSheet showInView:self.view];

            }
            else
            {

                UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Twitter" message:@"You have no twitter account" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
                alert.tag = 999;

                [alert show];

            }

            });

            }

}];
}

【讨论】:

    【解决方案2】:

    试试这个(你需要请求访问帐户)

    ACAccountStore *accountStore = [[ACAccountStore alloc] init];
    ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
    [accountStore requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error){
        if (granted) {
            NSArray *accounts = [accountStore accountsWithAccountType:accountType];
    
    }];
    

    【讨论】:

    • 您在设置中添加了推特帐户吗?应用是否向用户请求访问 Twitter 帐户?
    • 是的,我已经在设置中添加了 twitter 帐户 - 但没有向用户询问权限:“AppName”想要访问 twitter 帐户
    • 怎么强行问这个消息
    • accountStore requestAccessToAccountsWithType 应该请求权限。检查该应用是否存在于推特设置中推特名称下的列表中,并为此应用打开开关
    • requestAccessToAccountsWithType 调用会在一段时间后给出结果 - 因此帐户第一次不显示任何对象,但是当用户第二次触发此代码时,帐户数组会正确加载并显示结果
    猜你喜欢
    • 2014-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 2013-10-01
    • 1970-01-01
    相关资源
    最近更新 更多