为什么低于代码works fine in ios6
和not 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()
答案 0 :(得分:3)
我曾经这样做以获得一系列帐户,它确实给了我ios 6和ios 7。 尝试一下它的工作。谢谢。
在.h文件中导入这些框架
#import <Twitter/Twitter.h>
#import <Accounts/Accounts.h>
在.m文件中添加此ARC兼容代码任何按钮操作或您想要帐户数组 -
/*
// 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];
}
});
}
}];
}
答案 1 :(得分:1)
尝试此操作(您需要请求访问帐户)
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];
}];