使用iOS Social/Accounts框架检索Twitter用户数据

Bis*_*hop 4 social twitter ios slrequest

有没有办法使用iOS社交/帐户框架从Twitter获取用户数据(名字,姓氏和电子邮件)?我可以用Facebook做到这一点,但我对Twitter做的每一个SLRequest都返回一个空数组.

这是我现在的代码.我尝试了几个不同参数的URL,但我没有运气.

- (void)populateTwitterAccount  {
NSURL *twitterURL = [NSURL URLWithString:@"https://api.twitbridge.com/1.1/users/show.json"];

SLRequest *twitterRequest = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodGET URL:twitterURL parameters:nil];

[twitterRequest setAccount:self.twitterAccount];

[twitterRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
    NSString *accountDataString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];

    NSLog(@"%@", accountDataString);

}];
Run Code Online (Sandbox Code Playgroud)

}

Sha*_* BS 16

我们可以.

 ACAccountStore *account = [[ACAccountStore alloc] init];
 ACAccountType *accountType = [account accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
NSString *message = _textView.text;
//hear before posting u can allow user to select the account
 NSArray *arrayOfAccons = [account accountsWithAccountType:accountType];
 for(ACAccount *acc in arrayOfAccons)
 {
    NSLog(@"%@",acc.username); 
    NSDictionary *properties = [acc dictionaryWithValuesForKeys:[NSArray arrayWithObject:@"properties"]];
    NSDictionary *details = [properties objectForKey:@"properties"];
    NSLog(@"user name = %@",[details objectForKey:@"fullName"]);//full name
    NSLog(@"user_id  =  %@",[details objectForKey:@"user_id"]);//user id 
 } 
Run Code Online (Sandbox Code Playgroud)

电子邮件ID,我们也可以能够获得用户的电子邮件ID看到更新的答案听到


Tos*_*lji 8

是的,您可以使用获取用户信息ACAccountStore,您必须保留ACAccountStore: .h

@property (nonatomic, strong) ACAccountStore *account;
Run Code Online (Sandbox Code Playgroud)

.M

  NSUrl *url = [NSURL URLWithString:@"https://api.twitter.com/1.1/users/show.json"];
  NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:twittername,@"screen_name",nil];

     account = [[ACAccountStore alloc] init];
    ACAccountType *twitterAccountType = [account accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
    NSArray *twitterAccounts = [account accountsWithAccountType:twitterAccountType];

    // Runing on iOS 6
    if (NSClassFromString(@"SLComposeViewController") && [SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
    {
        [account requestAccessToAccountsWithType:twitterAccountType options:NULL completion:^(BOOL granted, NSError *error)
         {
             if (granted)
             {

                 SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodGET URL:url                                      parameters:params];

                 [request setAccount:[twitterAccounts lastObject]];

                 dispatch_async(dispatch_get_main_queue(), ^
                                {

                                    [NSURLConnection sendAsynchronousRequest:request.preparedURLRequest queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response1, NSData *data, NSError *error)
                                     {
                                         dispatch_async(dispatch_get_main_queue(), ^
                                                        {
                                                            if (data)
                                                            {
//                                                                [self loadData:data];

                                                                NSString* newStr = [[NSString alloc] initWithData:data
                                                                                                          encoding:NSUTF8StringEncoding];
                                                                NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] ;


                                                                NSLog(@"data:%@",newStr);
                                                            }
                                                        });
                                     }];
                                });
             }
         }];
    }
    else if (NSClassFromString(@"TWTweetComposeViewController") && [TWTweetComposeViewController canSendTweet]) // Runing on iOS 5
    {
        [account requestAccessToAccountsWithType:twitterAccountType withCompletionHandler:^(BOOL granted, NSError *error)
         {
             if (granted)
             {
                 TWRequest *request = [[TWRequest alloc] initWithURL:url parameters:params requestMethod:TWRequestMethodGET];
                 [request setAccount:[twitterAccounts lastObject]];

                 dispatch_async(dispatch_get_main_queue(), ^
                                {
                                    [NSURLConnection sendAsynchronousRequest:request.signedURLRequest queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response1, NSData *data, NSError *error)
                                     {
                                         dispatch_async(dispatch_get_main_queue(), ^
                                                        {                             
                                                            if (data)                                 
                                                            {                                 
                                                                NSString* newStr = [[NSString alloc] initWithData:data
                                                                                                         encoding:NSUTF8StringEncoding];


                                                                NSLog(@"data:%@",newStr);                                                           }
                                                        });
                                     }];


                                });
             }
         }];
    }
}
Run Code Online (Sandbox Code Playgroud)