使用api在IOS5中获取Twitter直接消息

时间:2012-11-20 12:59:24

标签: ios5 twitter twitter-oauth

如何使用api在IOS 5中获取Twitter直接消息。

1 个答案:

答案 0 :(得分:-1)

使用ID作为参数向您的关注者发送直接消息:

ACAccountStore *account = [[ACAccountStore alloc] init];
ACAccountType *accountType = [account accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];

// Request access from the user to access their Twitter account
[account requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error) {
    // Did user allow us access?
    if (granted == YES)
    {
        // Populate array with all available Twitter accounts
        NSArray *arrayOfAccounts = [account accountsWithAccountType:accountType];

        // Sanity check
        if ([arrayOfAccounts count] > 0) 
        {
            // Keep it simple, use the first account available
            ACAccount *acct = [arrayOfAccounts objectAtIndex:0];

            // Build a twitter request
            NSURL *url = [NSURL URLWithString:@"https://api.twitter.com/1/direct_messages/new.json"];

            NSMutableDictionary *params;

            for (int i =0 ; i <[friends_SelectedIDArray count]; i++) {

                params = [[NSMutableDictionary alloc] init];
                //[params setObject:@"@meenukatal" forKey:@"screen_name"];
                [params setObject:[friends_SelectedIDArray objectAtIndex:i] forKey:@"user_id"];
                //need to change sendimg message every time do not send same message //

                [params setObject:text_Field.text  forKey:@"text"];

                TWRequest *postRequest = [[TWRequest alloc]
                                          initWithURL:   url
                                          parameters:    params
                                          requestMethod: TWRequestMethodPOST
                                          ];

                // Post the request
                [postRequest setAccount:acct];

                // Block handler to manage the response
                [postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
                    NSLog(@"Twitter response, HTTP response: %i", [urlResponse statusCode]);
                    NSLog(@"Response Data\n%@", responseData);

                    if ([urlResponse statusCode] == 200) {

                        UIAlertView *alertmessage = [[UIAlertView alloc]initWithTitle:@"Message" message:@"You have successfully posted the message! " delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
                        [alertmessage show];

                    }

                    else {

                        UIAlertView *alertmessage = [[UIAlertView alloc]initWithTitle:@"Alert" message:@"There might be some problem please try later! " delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
                        [alertmessage show];

                    }


                    if (!error)
                        NSLog(@"%@", [error description]);
                }];
            }
        }
    }
}];
相关问题